Skip to content

Commit f2b70d5

Browse files
committed
fix runner name issue and req invalid log
1 parent 0bf672c commit f2b70d5

File tree

5 files changed

+83
-57
lines changed

5 files changed

+83
-57
lines changed

lib/component/json_schema.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@ async function writeJsonSchema(element, jsonSchemaPath, moduleType) {
1010
let name = (element.name).toLowerCase().replace(/\s/g, '');
1111
name = name.replace(/\//g, '');
1212
// A variable called 'method' is created and assigned the value obtained from the 'method' property of the 'element.request' object.
13-
let method = element.request.method;
13+
let method
14+
if (element.request.hasOwnProperty('method')) {
15+
method = element.request.method;
16+
}
1417
// check if file exists
15-
isFileExisted(jsonSchemaPath, method + '_' + name + '.schema.js')
16-
.then((data) => {
17-
if (!data[0]) {
18-
// create file test
19-
fs.writeFile(jsonSchemaPath + '/' + method + '_' +name + '.schema.js',
20-
fs.readFileSync(basePath() + templateDir, 'utf8'), function (err) { if (err) throw err; });
21-
}
22-
})
23-
.catch((err) => console.log(err));
18+
if (element.request.hasOwnProperty('url')) {
19+
isFileExisted(jsonSchemaPath, method + '_' + name + '.schema.js')
20+
.then((data) => {
21+
if (!data[0]) {
22+
// create file test
23+
fs.writeFile(jsonSchemaPath + '/' + method + '_' +name + '.schema.js',
24+
fs.readFileSync(basePath() + templateDir, 'utf8'), function (err) { if (err) throw err; });
25+
}
26+
})
27+
.catch((err) => console.log(err));
28+
}
2429
}
2530

2631
export default writeJsonSchema

lib/component/pages.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,6 @@ async function writePages(element, path, jsonSchemaRelativePath, helperPath, mod
127127
})
128128
await waitFor(50);
129129
dataQuery += ' })'
130-
} else {
131-
dataQuery = ''
132130
}
133131
}
134132
await waitFor(50);
@@ -155,15 +153,17 @@ async function writePages(element, path, jsonSchemaRelativePath, helperPath, mod
155153
await waitFor(50);
156154

157155
// check if pages file exists
158-
isFileExisted(path, method + '_' + name + '.pages.js')
159-
.then((data) => {
160-
if (!data[0]) {
161-
// create file test
162-
fs.writeFile(path + '/' + method + '_' + name + '.pages.js',
163-
code, function (err) { if (err) throw err; });
164-
}
165-
})
166-
.catch((err) => console.log(err));
156+
if (element.request.hasOwnProperty('url')) {
157+
isFileExisted(path, method + '_' + name + '.pages.js')
158+
.then((data) => {
159+
if (!data[0]) {
160+
// create file test
161+
fs.writeFile(path + '/' + method + '_' + name + '.pages.js',
162+
code, function (err) { if (err) throw err; });
163+
}
164+
})
165+
.catch((err) => console.log(err));
166+
}
167167
}
168168

169169
export default writePages

lib/component/runner.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,47 @@ async function writeRunner(element, testPath, runPath, moduleType) {
1919
let namet = (item.name).toLowerCase().replace(/\s/g, '')
2020
namet = namet.replace(/\//g, '')
2121
namet = namet.replace('-', '_')
22-
let method = item.request.method
22+
namet = namet.replace('(', '_')
23+
namet = namet.replace(')', '_')
2324

24-
if (moduleType == "Javascript modules (import/export)") {
25-
if (first === false) runner += '\r\n'
26-
runner += "import " + namet + " from '"+ testPath + '/' + method + '_' + namet + ".spec.js'"
27-
first = false;
25+
let method
26+
if (item.request.hasOwnProperty('method')) {
27+
method = item.request.method
28+
}
29+
if (item.request.hasOwnProperty('url')) {
30+
if (moduleType == "Javascript modules (import/export)") {
31+
if (first === false) runner += '\r\n'
32+
runner += "import " + namet + " from '"+ testPath + '/' + method + '_' + namet + ".spec.js'"
33+
first = false;
2834

29-
if (fisrtExe === false) importExe += '\r\n'
30-
importExe += namet + "()"
31-
fisrtExe = false;
32-
} else {
33-
if (first === false) runner += '\r\n'
34-
runner += "require('"+ testPath+'/'+method+'_'+namet+".spec')()"
35-
first = false;
35+
if (fisrtExe === false) importExe += '\r\n'
36+
importExe += namet + "()"
37+
fisrtExe = false;
38+
} else {
39+
if (first === false) runner += '\r\n'
40+
runner += "require('"+ testPath+'/'+method+'_'+namet+".spec')()"
41+
first = false;
42+
}
3643
}
3744
}
3845
})
3946
} else {
40-
let namet = (element.name).toLowerCase().replace(/\s/g, '');
41-
namet = namet.replace(/\//g, '');
42-
let method = element.request.method
47+
let namet = (element.name).toLowerCase().replace(/\s/g, '')
48+
namet = namet.replace(/\//g, '')
49+
namet = namet.replace('-', '_')
50+
namet = namet.replace('-', '_')
51+
namet = namet.replace('(', '_')
52+
namet = namet.replace(')', '_')
4353

44-
if (first === false) runner += '\r\n'
45-
runner += "require('"+ testPath+'/'+method+'_'+namet+".spec')()"
46-
first = false;
54+
let method
55+
if (element.request.hasOwnProperty('method')) {
56+
method = element.request.method
57+
}
58+
if (element.request.hasOwnProperty('url')) {
59+
if (first === false) runner += '\r\n'
60+
runner += "require('"+ testPath+'/'+method+'_'+namet+".spec')()"
61+
first = false;
62+
}
4763
}
4864
await waitFor(10)
4965
runner += '\r\n' + '\r\n' + importExe + '\r\n'

lib/component/tests.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,27 @@ let data = [
6969
code = code.replace("{{configPath}}", configPath)
7070
await waitFor(50);
7171

72-
7372
// check if file exists
74-
isFileExisted(path, method + '_' + name + '.spec.js')
75-
.then((data) => {
76-
if (!data[0]) {
77-
// create file test
78-
fs.writeFile(path + '/' + method + '_' + name + '.spec.js',
79-
code, function (err) { if (err) throw err; });
80-
81-
// _postman_isSubFolder
82-
console.log(`${'\x1b[32m'}ø Generate Test ${path + '/' + method + '_' + name + '.spec.js'} completed successfully${'\x1b[0m'}`)
83-
} else {
84-
// file was existed
85-
console.log(`${'\x1b[33m'}ø The request of ${element.name} has already created${'\x1b[0m'}`)
86-
}
87-
})
88-
.catch(err => console.log(err));
73+
if (element.request.hasOwnProperty('url')) {
74+
isFileExisted(path, method + '_' + name + '.spec.js')
75+
.then((data) => {
76+
if (!data[0]) {
77+
// create file test
78+
fs.writeFile(path + '/' + method + '_' + name + '.spec.js',
79+
code, function (err) { if (err) throw err; });
80+
81+
// _postman_isSubFolder
82+
console.log(`${'\x1b[32m'}ø Generate Test ${path + '/' + method + '_' + name + '.spec.js'} completed successfully${'\x1b[0m'}`)
83+
} else {
84+
// file was existed
85+
console.log(`${'\x1b[33m'}ø The request of ${element.name} has already created${'\x1b[0m'}`)
86+
}
87+
})
88+
.catch(err => console.log(err));
89+
} else {
90+
// invalid request
91+
console.log(`${'\x1b[31m'}ø ${element.name} was invalid request!${'\x1b[0m'}`)
92+
}
8993
}
9094

9195
export default writeTest

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "@dot.indonesia/po-gen",
3-
"version": "1.4.5",
3+
"version": "1.4.6",
44
"main": "index.js",
55
"scripts": {
66
"test": "echo \"Error: no test specified\" && exit 1",
7-
"test:regression-dev": "mocha runner/regression.js --timeout 15000"
7+
"test:regression-dev": "mocha runner/regression.js --timeout 15000",
8+
"test:dev": "cross-env NODE_ENV=dev mocha runner/regression.js --timeout 15000"
89
},
910
"type": "module",
1011
"bin": {

0 commit comments

Comments
 (0)