Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct regex match condition in SyntaxValidator #2384

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/imperative/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to the Imperative package will be documented in this file.

## Recent Changes

- BugFix: Fixed a typo in the syntax validation code for positional arguments which caused the validation to never fail. [#2375](https://github.com/zowe/zowe-cli/issues/2375)

## `8.10.1`

Expand Down
2 changes: 1 addition & 1 deletion packages/imperative/src/cmd/src/syntax/SyntaxValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export class SyntaxValidator {
if (!(commandArguments[positional.name] == null)) {
if (positional.regex) {
if (commandArguments[positional.name]
.toString().match(new RegExp(positional.regex) == null)) {
.toString().match(new RegExp(positional.regex)) == null) {
valid = false;
this.positionalParameterInvalid(positional,
commandArguments[positional.name], responseObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
if (!(expectedText == null) && expectedText.length > 0) {
const fullText = response.buildJsonResponse().stdout.toString() + response.buildJsonResponse().stderr.toString();
for (const text of expectedText) {
expect(fullText).toContain(text);

Check failure on line 71 in packages/imperative/src/cmd/src/syntax/__tests__/SyntaxValidator.unit.test.ts

View workflow job for this annotation

GitHub Actions / test (18.x, macos-14)

Imperative should provide advanced syntax validation rules › Advanced syntax validation for commands using a test command › should fail if a positional argument does not match the defined regex

expect(received).toContain(expected) // indexOf Expected substring: "Positional argument 'invalid_value' does not match the regex: ^[a-zA-Z0-9_]+$" Received string: " Syntax Error: You specified the following unknown values: \"invalid_value\".· Could not interpret them as a group, command name, or positional option. " at packages/imperative/src/cmd/src/syntax/__tests__/SyntaxValidator.unit.test.ts:71:46

Check failure on line 71 in packages/imperative/src/cmd/src/syntax/__tests__/SyntaxValidator.unit.test.ts

View workflow job for this annotation

GitHub Actions / test (18.x, ubuntu-22.04)

Imperative should provide advanced syntax validation rules › Advanced syntax validation for commands using a test command › should fail if a positional argument does not match the defined regex

expect(received).toContain(expected) // indexOf Expected substring: "Positional argument 'invalid_value' does not match the regex: ^[a-zA-Z0-9_]+$" Received string: " Syntax Error: You specified the following unknown values: \"invalid_value\".· Could not interpret them as a group, command name, or positional option. " at packages/imperative/src/cmd/src/syntax/__tests__/SyntaxValidator.unit.test.ts:71:46

Check failure on line 71 in packages/imperative/src/cmd/src/syntax/__tests__/SyntaxValidator.unit.test.ts

View workflow job for this annotation

GitHub Actions / test (18.x, windows-latest)

Imperative should provide advanced syntax validation rules › Advanced syntax validation for commands using a test command › should fail if a positional argument does not match the defined regex

expect(received).toContain(expected) // indexOf Expected substring: "Positional argument 'invalid_value' does not match the regex: ^[a-zA-Z0-9_]+$" Received string: " Syntax Error: You specified the following unknown values: \"invalid_value\".· Could not interpret them as a group, command name, or positional option. " at packages/imperative/src/cmd/src/syntax/__tests__/SyntaxValidator.unit.test.ts:71:46

Check failure on line 71 in packages/imperative/src/cmd/src/syntax/__tests__/SyntaxValidator.unit.test.ts

View workflow job for this annotation

GitHub Actions / test (20.x, macos-14)

Imperative should provide advanced syntax validation rules › Advanced syntax validation for commands using a test command › should fail if a positional argument does not match the defined regex

expect(received).toContain(expected) // indexOf Expected substring: "Positional argument 'invalid_value' does not match the regex: ^[a-zA-Z0-9_]+$" Received string: " Syntax Error: You specified the following unknown values: \"invalid_value\".· Could not interpret them as a group, command name, or positional option. " at packages/imperative/src/cmd/src/syntax/__tests__/SyntaxValidator.unit.test.ts:71:46

Check failure on line 71 in packages/imperative/src/cmd/src/syntax/__tests__/SyntaxValidator.unit.test.ts

View workflow job for this annotation

GitHub Actions / test (20.x, ubuntu-22.04)

Imperative should provide advanced syntax validation rules › Advanced syntax validation for commands using a test command › should fail if a positional argument does not match the defined regex

expect(received).toContain(expected) // indexOf Expected substring: "Positional argument 'invalid_value' does not match the regex: ^[a-zA-Z0-9_]+$" Received string: " Syntax Error: You specified the following unknown values: \"invalid_value\".· Could not interpret them as a group, command name, or positional option. " at packages/imperative/src/cmd/src/syntax/__tests__/SyntaxValidator.unit.test.ts:71:46

Check failure on line 71 in packages/imperative/src/cmd/src/syntax/__tests__/SyntaxValidator.unit.test.ts

View workflow job for this annotation

GitHub Actions / test (20.x, windows-latest)

Imperative should provide advanced syntax validation rules › Advanced syntax validation for commands using a test command › should fail if a positional argument does not match the defined regex

expect(received).toContain(expected) // indexOf Expected substring: "Positional argument 'invalid_value' does not match the regex: ^[a-zA-Z0-9_]+$" Received string: " Syntax Error: You specified the following unknown values: \"invalid_value\".· Could not interpret them as a group, command name, or positional option. " at packages/imperative/src/cmd/src/syntax/__tests__/SyntaxValidator.unit.test.ts:71:46
}
}
// done();
Expand Down Expand Up @@ -525,6 +525,19 @@
expect(svResponse.valid).toEqual(true);
});

it("should fail if a positional argument does not match the defined regex", async () => {
const invalidPositional = "invalid_value";
const regexForPositional = "^[a-zA-Z0-9_]+$";

return tryOptions.bind(
this,
`${minValidOptions} ${invalidPositional}`,
false,
[`Positional argument '${invalidPositional}' does not match the regex: ${regexForPositional}`]
)();
});


describe("We should be able to validate positional arguments of type 'number'", () => {
const numberCommand: ICommandDefinition = {
name: "gimme-number", aliases: [],
Expand Down
Loading