Skip to content

Commit 2bc8ed9

Browse files
authored
fix: Maven command testing does not work correctly (#1100)
* fix: Maven command testing does not work correctly Signed-off-by: Chad Wilson <29788154+chadlwilson@users.noreply.github.com> * chore: tidy constants for Maven/Gradle for each of import Signed-off-by: Chad Wilson <29788154+chadlwilson@users.noreply.github.com> --------- Signed-off-by: Chad Wilson <29788154+chadlwilson@users.noreply.github.com>
1 parent ecb856b commit 2bc8ed9

File tree

9 files changed

+81
-45
lines changed

9 files changed

+81
-45
lines changed

package-lock.json

Lines changed: 21 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"description": "Gauge support for VScode.",
99
"author": "ThoughtWorks",
1010
"license": "MIT",
11-
"version": "0.2.3",
11+
"version": "0.2.4",
1212
"publisher": "getgauge",
1313
"engines": {
1414
"vscode": "^1.82.0"

src/cli.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { ChildProcess, CommonSpawnOptions, spawn, spawnSync, SpawnSyncReturns } from 'child_process';
44
import { platform } from 'os';
55
import { window } from 'vscode';
6-
import { GaugeCommands, GRADLE_COMMAND, MAVEN_COMMAND } from './constants';
6+
import { GaugeCommands, GradleCommands, MavenCommands } from './constants';
77
import { OutputChannel } from './execution/outputChannel';
88

99
export class CLI {
@@ -25,7 +25,7 @@ export class CLI {
2525

2626
public static instance(): CLI {
2727
const gaugeCommand = this.getCommand(GaugeCommands.Gauge);
28-
const mvnCommand = this.getCommand(MAVEN_COMMAND);
28+
const mvnCommand = this.getCommand(MavenCommands.Command, [MavenCommands.CommandTestArg]);
2929
const gradleCommand = this.getGradleCommand();
3030
if (!gaugeCommand) return new CLI(undefined, {}, mvnCommand, gradleCommand);
3131
let gv = gaugeCommand.spawnSync([GaugeCommands.Version, GaugeCommands.MachineReadable]);
@@ -101,19 +101,21 @@ export class CLI {
101101
]
102102
}
103103

104-
public static isSpawnable(command: Command): boolean {
105-
const result = command.spawnSync();
104+
public static isSpawnable(command: Command, testArgs: string[] = []): boolean {
105+
const result = command.spawnSync(testArgs);
106106
return result.status === 0 && !result.error;
107107
}
108108

109-
private static getCommand(command: string): Command | undefined {
109+
private static getCommand(command: string, testArgs: string[] = []): Command | undefined {
110110
for (const candidate of this.getCommandCandidates(command)) {
111-
if (this.isSpawnable(candidate)) return candidate;
111+
if (this.isSpawnable(candidate, testArgs)) return candidate;
112112
}
113113
}
114114

115115
private static getGradleCommand() {
116-
return platform() === 'win32' ? new Command(GRADLE_COMMAND, ".bat", true) : new Command(`./${GRADLE_COMMAND}`);
116+
return platform() === 'win32'
117+
? new Command(GradleCommands.WrapperCommand, ".bat", true)
118+
: new Command(`./${GradleCommands.WrapperCommand}`);
117119
}
118120
}
119121

src/constants.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,21 @@ export enum GaugeRunners {
7373
Dotnet = "dotnet"
7474
}
7575

76+
export enum MavenCommands {
77+
Command = 'mvn',
78+
CommandTestArg = '--version',
79+
PomFile = 'pom.xml',
80+
}
81+
82+
export enum GradleCommands {
83+
WrapperCommand = 'gradlew',
84+
BuildFile = 'build.gradle',
85+
}
86+
7687
export const LAST_REPORT_PATH = 'gauge.execution.report';
7788
export const COPY_TO_CLIPBOARD = 'Copy To Clipboard';
7889
export const NEW_FILE = 'New File';
79-
export const GAUGE_TEMPLATE_URL = 'https://downloads.gauge.org/templates';
8090
export const GAUGE_MANIFEST_FILE = 'manifest.json';
81-
export const MAVEN_POM = "pom.xml";
82-
export const MAVEN_COMMAND = "mvn";
83-
export const GRADLE_COMMAND = "gradlew";
84-
export const GRADLE_BUILD = 'build.gradle';
8591
export const GAUGE_CUSTOM_CLASSPATH = 'gauge_custom_classpath';
8692
export const GAUGE_DOCS_URI = 'https://docs.gauge.org';
8793
export const INSTALL_INSTRUCTION_URI = `${GAUGE_DOCS_URI}/getting_started/installing-gauge.html`;

src/project/projectFactory.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { existsSync, readFileSync } from "fs";
22
import { join, parse } from "path";
3-
import { GAUGE_MANIFEST_FILE, MAVEN_POM, GRADLE_BUILD } from "../constants";
3+
import { GAUGE_MANIFEST_FILE, MavenCommands, GradleCommands } from "../constants";
44
import { GaugeProject } from "./gaugeProject";
55
import { MavenProject } from "./mavenProject";
66
import { GradleProject } from "./gradleProject";
@@ -11,11 +11,11 @@ export class ProjectFactory {
1111
build: (root: string, data: string) => GaugeProject
1212
}> = [
1313
{
14-
predicate: (root: string) => existsSync(join(root, MAVEN_POM)),
14+
predicate: (root: string) => existsSync(join(root, MavenCommands.PomFile)),
1515
build: (root: string, data: any) => new MavenProject(root, data)
1616
},
1717
{
18-
predicate: (root: string) => existsSync(join(root, GRADLE_BUILD)),
18+
predicate: (root: string) => existsSync(join(root, GradleCommands.BuildFile)),
1919
build: (root: string, data: any) => new GradleProject(root, data)
2020
}
2121
];

test/cli.test.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as assert from 'assert';
22
import { CLI, Command } from '../src/cli';
33
import path = require('path');
4-
import { spawnSync } from "child_process";
54

65
let testCommandsPath = path.join(__dirname, '..', '..', 'test', 'commands');
76

@@ -149,12 +148,11 @@ ruby (1.2.0)`;
149148
});
150149

151150
test('.getCommandCandidates choices all valid by .isSpawnable', (done) => {
152-
let candidates = CLI.getCommandCandidates('test_command');
153151
const originalPath = process.env.PATH;
154152
process.env.PATH = testCommandsPath;
155153
let invalid_candidates = [];
156154
try {
157-
for (const candidate of candidates) {
155+
for (const candidate of CLI.getCommandCandidates('test_command')) {
158156
if (!CLI.isSpawnable(candidate)) {
159157
invalid_candidates.push(candidate);
160158
}
@@ -166,13 +164,26 @@ ruby (1.2.0)`;
166164
done();
167165
});
168166

169-
test('.getCommandCandidates choices can be found as in valid via .isSpawnable', (done) => {
170-
let candidates = CLI.getCommandCandidates('test_command_not_found');
167+
test('.getCommandCandidates choices all valid by .isSpawnable when requiring an arg', (done) => {
168+
const originalPath = process.env.PATH;
169+
process.env.PATH = testCommandsPath;
170+
try {
171+
for (const candidate of CLI.getCommandCandidates('test_command_needs_version_arg').filter(c => c.cmdSuffix !== ".exe")) {
172+
assert.ok(!CLI.isSpawnable(candidate), `candidate ${candidate.command} should not be spawnable without --version arg`);
173+
assert.ok(CLI.isSpawnable(candidate, ["--version"]), `candidate ${candidate.command} should be spawnable with --version arg`);
174+
}
175+
} finally {
176+
process.env.PATH = originalPath;
177+
}
178+
done();
179+
});
180+
181+
test('.getCommandCandidates choices can be found as invalid via .isSpawnable', (done) => {
171182
const originalPath = process.env.PATH;
172183
process.env.PATH = testCommandsPath;
173184
let valid_candidates = [];
174185
try {
175-
for (const candidate of candidates) {
186+
for (const candidate of CLI.getCommandCandidates('test_command_not_found')) {
176187
if (CLI.isSpawnable(candidate)) {
177188
valid_candidates.push(candidate);
178189
}
@@ -185,11 +196,10 @@ ruby (1.2.0)`;
185196
});
186197

187198
test('.getCommandCandidates can be spawned with an arg', (done) => {
188-
let candidates = CLI.getCommandCandidates('test_command');
189199
const originalPath = process.env.PATH;
190200
process.env.PATH = testCommandsPath;
191201
try {
192-
for (const candidate of candidates.filter(c => (c.cmdSuffix !== ".exe"))) {
202+
for (const candidate of CLI.getCommandCandidates('test_command').filter(c => c.cmdSuffix !== ".exe")) {
193203
const result = candidate.spawnSync(["Hello World"]);
194204
assert.ok(result.status === 0 && !result.error, `Command candidate ${candidate.command} failed to spawn`);
195205
assert.equal(result.stdout.toString().trim(), 'Success: "Hello World"', `Command candidate ${candidate.command} has wrong output`)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
if [ "--version" != "${1}" ]; then
3+
echo "Error: --version argument is required."
4+
exit 1
5+
fi
6+
echo "Success: \"${1}\""
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@echo off
2+
if not "%1" == "--version" (
3+
echo Error: --version argument is required
4+
exit /b 1
5+
)
6+
echo Success: %1
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@echo off
2+
if not "%1" == "--version" (
3+
echo Error: --version argument is required
4+
exit /b 1
5+
)
6+
echo Success: %1

0 commit comments

Comments
 (0)