Skip to content

Commit 293d848

Browse files
author
Vladislav Yevtushenko
committed
fix: parse-version exists func to minimize return statements
in order to address the code review comments
1 parent 122d9a0 commit 293d848

File tree

2 files changed

+61
-79
lines changed

2 files changed

+61
-79
lines changed

src/parser-version.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,21 @@ const request = require('sync-request'),
3232
* @return {Boolean} True if version exists, false otherwise
3333
*/
3434
exists(ver) {
35-
if (!ver || ver === 'undefined') {
36-
return false;
37-
}
38-
try {
39-
const repo = 'org/eolang/eo-maven-plugin',
40-
artifactId = 'eo-maven-plugin',
41-
url = `https://repo.maven.apache.org/maven2/${repo}/${ver}/${artifactId}-${ver}.pom`,
42-
res = request('GET', url, {timeout: 10000, socketTimeout: 10000});
43-
return res.statusCode === 200;
44-
} catch (e) {
45-
console.debug('Unable to validate parser version (network error): %s', e.message);
46-
return true;
35+
let result;
36+
if (ver && ver !== 'undefined') {
37+
try {
38+
const repo = 'org/eolang/eo-maven-plugin',
39+
artifactId = 'eo-maven-plugin',
40+
url = `https://repo.maven.apache.org/maven2/${repo}/${ver}/${artifactId}-${ver}.pom`,
41+
res = request('GET', url, {timeout: 10000, socketTimeout: 10000});
42+
result = res.statusCode === 200;
43+
} catch (e) {
44+
console.debug('Unable to validate parser version (network error): %s', e.message);
45+
result = false;
46+
}
47+
} else {
48+
result = false;
4749
}
50+
return result;
4851
}
4952
};

test/commands/test_parse.js

Lines changed: 46 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
const assert = require('assert');
77
const fs = require('fs');
88
const path = require('path');
9-
const {execSync} = require('child_process');
109
const {runSync, assertFilesExist, parserVersion, homeTag, weAreOnline} = require('../helpers');
1110

1211
describe('parse', () => {
@@ -21,26 +20,6 @@ describe('parse', () => {
2120
);
2221
};
2322

24-
const runCommand = (args) => {
25-
try {
26-
return execSync(
27-
`node ${path.resolve('./src/eoc.js')} --batch ${args}`,
28-
{
29-
timeout: 30000,
30-
windowsHide: true,
31-
encoding: 'utf8'
32-
}
33-
);
34-
} catch (ex) {
35-
return {
36-
stdout: ex.stdout ? ex.stdout.toString() : '',
37-
stderr: ex.stderr ? ex.stderr.toString() : '',
38-
error: ex.message,
39-
status: ex.status
40-
};
41-
}
42-
};
43-
4423
it('parses a simple .EO program', (done) => {
4524
const home = path.resolve('temp/test-parse/simple');
4625
createTestProject(home);
@@ -67,79 +46,79 @@ describe('parse', () => {
6746
this.timeout(30000);
6847
const home = path.resolve('temp/test-parse/invalid-version');
6948
createTestProject(home);
70-
71-
const result = runCommand([
72-
'parse',
73-
'--parser=999.999.999',
74-
'-s', path.resolve(home, 'src'),
75-
'-t', path.resolve(home, 'target'),
76-
].join(' '));
77-
78-
assert(result.status !== 0, 'Command should fail with non-zero exit code');
49+
assert.throws(
50+
() => {
51+
runSync([
52+
'parse',
53+
'--parser=999.999.999',
54+
'-s', path.resolve(home, 'src'),
55+
'-t', path.resolve(home, 'target'),
56+
]);
57+
},
58+
'Command should fail with invalid parser version'
59+
);
7960
});
8061

8162
it('accepts valid parser version', function () {
8263
this.timeout(60000);
8364
const home = path.resolve('temp/test-parse/valid-version');
8465
createTestProject(home);
85-
86-
const result = runCommand([
66+
runSync([
8767
'parse',
8868
'--parser=0.28.11',
8969
'-s', path.resolve(home, 'src'),
9070
'-t', path.resolve(home, 'target'),
91-
].join(' '));
92-
93-
if (typeof result === 'object' && result.status !== undefined) {
94-
assert(
95-
result.status === 0,
96-
'Command should succeed with valid parser version'
97-
);
98-
}
71+
]);
9972
});
10073

10174
it('validates parser version before calling Maven', function () {
10275
this.timeout(30000);
10376
const home = path.resolve('temp/test-parse/early-validation');
10477
createTestProject(home);
105-
106-
const result = runCommand([
107-
'parse',
108-
'--parser=999.999.999',
109-
'-s', path.resolve(home, 'src'),
110-
'-t', path.resolve(home, 'target'),
111-
].join(' '));
112-
113-
assert(result.status !== 0, 'Command should fail with invalid version');
78+
assert.throws(
79+
() => {
80+
runSync([
81+
'parse',
82+
'--parser=999.999.999',
83+
'-s', path.resolve(home, 'src'),
84+
'-t', path.resolve(home, 'target'),
85+
]);
86+
},
87+
'Command should fail with invalid version'
88+
);
11489
});
11590

11691
it('handles assemble command with invalid version', function () {
11792
this.timeout(30000);
11893
const home = path.resolve('temp/test-parse/assemble-invalid');
11994
createTestProject(home);
120-
121-
const result = runCommand([
122-
'assemble',
123-
'--parser=999.999.999',
124-
'-s', path.resolve(home, 'src'),
125-
'-t', path.resolve(home, 'target'),
126-
].join(' '));
127-
128-
assert(result.status !== 0, 'Assemble should fail with invalid version');
95+
assert.throws(
96+
() => {
97+
runSync([
98+
'assemble',
99+
'--parser=999.999.999',
100+
'-s', path.resolve(home, 'src'),
101+
'-t', path.resolve(home, 'target'),
102+
]);
103+
},
104+
'Assemble should fail with invalid version'
105+
);
129106
});
130107

131108
it('handles lint command with invalid version', function () {
132109
this.timeout(30000);
133110
const home = path.resolve('temp/test-parse/lint-invalid');
134111
createTestProject(home);
135-
136-
const result = runCommand([
137-
'lint',
138-
'--parser=999.999.999',
139-
'-s', path.resolve(home, 'src'),
140-
'-t', path.resolve(home, 'target'),
141-
].join(' '));
142-
143-
assert(result.status !== 0, 'Lint should fail with invalid version');
112+
assert.throws(
113+
() => {
114+
runSync([
115+
'lint',
116+
'--parser=999.999.999',
117+
'-s', path.resolve(home, 'src'),
118+
'-t', path.resolve(home, 'target'),
119+
]);
120+
},
121+
'Lint should fail with invalid version'
122+
);
144123
});
145124
});

0 commit comments

Comments
 (0)