Skip to content

Commit b2ad75b

Browse files
authored
Merge pull request #178 from asad9711/MLE-10219
Mle 10219
2 parents 9f8a5fa + 3f415f9 commit b2ad75b

File tree

8 files changed

+126
-35
lines changed

8 files changed

+126
-35
lines changed

commands/init.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
var fs = require('fs');
2-
var path = require('path')
2+
var path = require('path');
3+
const { exit } = require('process');
34
const { config } = require('yargs');
45
const constants = require('./utils/constants.js')
56

67
function create_file(file_path, content) {
78
fs.writeFile(file_path, content, function (err) {
89
if (err) throw err;
9-
console.log('Saved at ', file_path);
10+
console.log('Saved at ',file_path);
1011
});
1112
}
1213

@@ -42,7 +43,41 @@ function create_ltconfig_file(args) {
4243
}
4344
};
4445

46+
function create_base_reporter_config_file(args) {
47+
let config = require('./utils/default_reporter_config.js')
48+
let content = JSON.stringify(config, null, 3);
49+
if (args._.length == 1) {
50+
create_file(constants.LT_BASE_REPORTER_CONFIG_FILE_NAME, content)
51+
}
52+
// else if (args._.length > 1) {
53+
// //check if file or directory exists
54+
// if (fs.existsSync(args._[1])) {
55+
// let stats = fs.statSync(args._[1]);
56+
// if (stats.isFile()) {
57+
// make_file(args._[1], content)
58+
// }
59+
// else {
60+
// create_file(path.join(args._[1], constants.LT_CONFIG_NAME), content)
61+
// }
62+
// }
63+
// else {
64+
// filename = path.basename(args._[1])
65+
// var re = new RegExp(".+\\..+");
66+
// if (re.test(filename)) {
67+
// fs.mkdirSync(path.dirname(args._[1]), { recursive: true });
68+
// create_file(args._[1], content)
69+
// }
70+
// else {
71+
// fs.mkdirSync(args._[1], { recursive: true });
72+
// create_file(path.join(args._[1], constants.LT_CONFIG_NAME), content)
73+
// }
74+
// }
75+
// }
76+
};
77+
4578
module.exports = function (args) {
46-
create_ltconfig_file(args)
79+
80+
create_ltconfig_file(args);
81+
create_base_reporter_config_file(args);
4782

4883
};

commands/run.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,25 @@ module.exports = function (args) {
6868
.then(function (lt_config) {
6969
//validate the config options
7070
validate(lt_config, resp)
71-
.then(function () {
71+
.then(function (cypressVersion) {
72+
console.log("cypressVersion requested - ", cypressVersion);
73+
74+
/*
75+
update ltconfig to contain the cypress_version
76+
case 1: user passed cypress_version in run_settings, this case will work as earlier
77+
case 2: user hasn't passed cypress_version in run_settting, then also we will pass it, so that we can track this parameter in further services
78+
*/
79+
80+
81+
/* TEST scenarios:
82+
- user passes cypress_version in run_settings with both cases- with semver/without semver
83+
- user doesnot pass cypress_version in run_settings
84+
*/
85+
86+
if (!("cypress_version" in lt_config.run_settings)){
87+
console.log("user has not passed cypress version in run_SEttings");
88+
lt_config.run_settings.cypress_version = cypressVersion;
89+
}
7290
batcher
7391
.make_batches(lt_config)
7492
.then(function (batches) {

commands/utils/batch/batch_runner.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ function run_test(payload, env = "prod", rejectUnauthorized) {
7878
});
7979
}
8080

81-
async function run(lt_config, batches, env, i = 0) {
81+
async function run(lt_config, batches, env) {
82+
console.log("ltconfig ---- ", lt_config);
8283
totalBatches = batches.length;
8384
//console.log("Total number of batches " + totalBatches);
8485
return new Promise(function (resolve, reject) {
@@ -115,7 +116,7 @@ async function run(lt_config, batches, env, i = 0) {
115116
},
116117
username: lt_config["lambdatest_auth"]["username"],
117118
access_key: lt_config["lambdatest_auth"]["access_key"],
118-
type: "cypress",
119+
type: "cypress"
119120
});
120121

121122
run_test(

commands/utils/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
PROJECT_UPLOAD_URL: "/url",
66
CLI: "/cy/versions",
77
LT_CONFIG_NAME: "lambdatest-config.json",
8+
LT_BASE_REPORTER_CONFIG_FILE_NAME: "base_reporter_config.json",
89
CYPRESS_CONFIG_NAME: "cypress.json",
910
DEFAULT_TEST_PATH: ".",
1011
LAMBDA_CONFIG: "./lambdatest-config.json",

commands/utils/default_config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = {
1717
],
1818
run_settings: {
1919
cypress_config_file: "cypress.json",
20+
reporter_config_file: "base_reporter_config.json",
2021
build_name: "build-name",
2122
parallels: 1,
2223
specs: "./*.spec.js",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
"reporterEnabled": "mochawesome",
3+
"mochawesomeReporterOptions": {
4+
"reportDir": "cypress/results/json",
5+
"overwrite": true,
6+
"html": true,
7+
"json": true
8+
}
9+
};

commands/utils/set_args.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,13 @@ function sync_args_from_cmd(args) {
221221
lt_config["run_settings"]["ignore_files"] =
222222
args["ignore_files"].split(",");
223223
}
224+
225+
// if reporter_config_file parameter, add it in lt config alongwith a warning on console
226+
if (!lt_config["run_settings"]["reporter_config_file"]) {
227+
lt_config["run_settings"]["reporter_config_file"] = constants.LT_BASE_REPORTER_CONFIG_FILE_NAME;
228+
}
229+
230+
224231
if ("cypress_version" in args) {
225232
lt_config["run_settings"]["cypress_version"] = args["cypress_version"];
226233
} else if (lt_config["run_settings"]["cypress_version"]) {

commands/utils/validate.js

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
const fs = require("fs");
2+
const semver = require("semver");
3+
const semverCompare = require('semver/functions/compare');
4+
25
const constants = require("./constants.js");
36
module.exports = validate_config = function (lt_config, validation_configs) {
47
console.log("validating config");
@@ -48,29 +51,9 @@ module.exports = validate_config = function (lt_config, validation_configs) {
4851
reject("Error!! Parallels value not correct");
4952
}
5053

51-
//validate if cypress config file is passed and exists
52-
if (
53-
lt_config["run_settings"]["cypress_config_file"] &&
54-
lt_config["run_settings"]["cypress_config_file"] != ""
55-
) {
56-
if (!fs.existsSync(lt_config["run_settings"]["cypress_config_file"])) {
57-
reject("Error!! Cypress Config File does not exist");
58-
} else {
59-
let rawdata = fs.readFileSync(
60-
lt_config["run_settings"]["cypress_config_file"]
61-
);
62-
try {
63-
let cypress_config = JSON.parse(rawdata);
64-
} catch {
65-
console.log(
66-
"Cypress.json is not parsed, please provide a valid json"
67-
);
68-
reject("Error!! Cypress Config File does not has correct json");
69-
}
70-
}
71-
}
72-
54+
7355
//Validate if package.json is having the cypress dependency
56+
var cypress_version;
7457
if (!fs.existsSync("package.json")) {
7558
reject(
7659
"Error!! Package.json file does not exist in the root on the project"
@@ -89,6 +72,7 @@ module.exports = validate_config = function (lt_config, validation_configs) {
8972
for (const [key, value] of Object.entries(package["dependencies"])) {
9073
if (key == "cypress") {
9174
cypress_flag = true;
75+
cypress_version = value;
9276
break;
9377
}
9478
}
@@ -100,9 +84,9 @@ module.exports = validate_config = function (lt_config, validation_configs) {
10084
for (const [key, value] of Object.entries(
10185
package["devDependencies"]
10286
)) {
103-
console.log(key, value);
10487
if (key == "cypress") {
10588
cypress_flag = true;
89+
cypress_version = value;
10690
break;
10791
}
10892
}
@@ -112,6 +96,7 @@ module.exports = validate_config = function (lt_config, validation_configs) {
11296
lt_config.run_settings.cypress_version != ""
11397
) {
11498
cypress_flag = true;
99+
cypress_version = lt_config.run_settings.cypress_version;
115100
} else if (
116101
lt_config.run_settings.hasOwnProperty("cypress_version") &&
117102
lt_config.run_settings.cypress_version == ""
@@ -133,10 +118,38 @@ module.exports = validate_config = function (lt_config, validation_configs) {
133118
"Package.json is not parsed, please provide a valid json",
134119
e
135120
);
136-
reject("Error!! Package.json File does not has correct json");
121+
reject("Error!! Package.json File does not have correct json");
137122
}
138123
}
139124

125+
//validate if cypress config file is passed and exists
126+
127+
// coerce cypress_version
128+
cypress_version = semver.coerce(cypress_version).version;
129+
// validate cypress.json only in case of cypress<10
130+
if (
131+
semverCompare(cypress_version, "10.0.0") == -1 &&
132+
lt_config["run_settings"]["cypress_config_file"] &&
133+
lt_config["run_settings"]["cypress_config_file"] != ""
134+
) {
135+
if (!fs.existsSync(lt_config["run_settings"]["cypress_config_file"])) {
136+
reject("Error!! Cypress Config File does not exist");
137+
} else {
138+
let rawdata = fs.readFileSync(
139+
lt_config["run_settings"]["cypress_config_file"]
140+
);
141+
try {
142+
let cypress_config = JSON.parse(rawdata);
143+
} catch {
144+
console.log(
145+
"Cypress.json is not parsed, please provide a valid json"
146+
);
147+
reject("Error!! Cypress Config File does not has correct json");
148+
}
149+
}
150+
}
151+
152+
140153
if (
141154
lt_config["run_settings"]["ignore_files"] &&
142155
lt_config["run_settings"]["ignore_files"].length > 0
@@ -208,14 +221,13 @@ module.exports = validate_config = function (lt_config, validation_configs) {
208221
reject("Smart UI porject name can not be blank");
209222
}
210223
}
211-
//validate if reporter json file is passed and exists
212224
if (
213225
lt_config["run_settings"]["reporter_config_file"] &&
214226
lt_config["run_settings"]["reporter_config_file"] != ""
215227
) {
216228
if (!fs.existsSync(lt_config["run_settings"]["reporter_config_file"])) {
217229
reject(
218-
"Error!! Reporter Json File does not exist, either remove the key reporter_config_file or pass a valid path"
230+
"Error!! Reporter Config File does not exist, Pass a valid path"
219231
);
220232
} else {
221233
let rawdata = fs.readFileSync(
@@ -227,14 +239,21 @@ module.exports = validate_config = function (lt_config, validation_configs) {
227239
reject(
228240
"Error!! Reporter JSON File has no keys, either remove Key reporter_config_file from lambdatest config or pass some options"
229241
);
242+
}else if (reporter_config.reporterEnabled && reporter_config.reporterEnabled != ""){
243+
console.log("reporter_config.reporterEnabled ", reporter_config.reporterEnabled);
244+
if (!reporter_config.reporterEnabled.includes("mochawesome")){
245+
reject("Error!! Please add mochawesome in reporterEnabled and add the related config");
246+
}
230247
}
231248
} catch {
232249
console.log(
233-
"reporter_config_file is not parsed, please provide a valid json in Reporter Config"
250+
"reporter_config_file can not parsed, please provide a valid json in Reporter Config"
234251
);
235-
reject("Error!! Reporter JSON File does not has correct json");
252+
reject("Error!! Reporter JSON File does not have correct json");
236253
}
237254
}
255+
}else{
256+
console.log("Warning !! Value of reporter_config_file parameter missing. Proceeding with default reporter config")
238257
}
239258

240259
if (
@@ -336,6 +355,6 @@ module.exports = validate_config = function (lt_config, validation_configs) {
336355
})
337356

338357
}
339-
resolve("Validated the Config");
358+
resolve(cypress_version);
340359
});
341360
};

0 commit comments

Comments
 (0)