Skip to content

Commit

Permalink
fix(cli): respect --exit regardless of other flags (#750)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdnoC authored Jun 16, 2023
1 parent 8a9a92d commit bfa2328
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
18 changes: 18 additions & 0 deletions packages/cli/src/bin/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import { version } from '../../package.json';
import runCLI from '../testutils/';

const SIMPLE_HTML_FILE = path.join(__dirname, '..', 'testutils', 'simple.html');
const SIMPLE_CLEAN_HTML_FILE = path.join(
__dirname,
'..',
'testutils',
'simple-clean.html'
);
const SIMPLE_HTML_SOURCE = fs.readFileSync(SIMPLE_HTML_FILE, 'utf8');
const PATH_TO_AXE_250 = path.resolve(
__dirname,
Expand Down Expand Up @@ -147,6 +153,18 @@ describe('cli', () => {
);
}
});

it('should exit zero if violations are found', async () => {
try {
await runCLI(`file://${SIMPLE_CLEAN_HTML_FILE}`, '--exit');
} catch (error) {
assert.equal(error.exitCode, 0);
assert.include(
error.stdout,
'Violation of "marquee" with 1 occurrences!'
);
}
});
});

describe('--dir', () => {
Expand Down
18 changes: 18 additions & 0 deletions packages/cli/src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,24 @@ const cli = async (
}
}

if (exit) {
let exitErr = false;
/* istanbul ignore if */
if (Array.isArray(outcome)) {
for (const res of outcome) {
if (res.violations.length > 0) {
exitErr = true;
break;
}
}
} else {
exitErr = outcome.violations.length > 0;
}
if (exitErr) {
process.exit(1);
}
}

/* istanbul ignore if */
if (silentMode) {
return;
Expand Down
4 changes: 0 additions & 4 deletions packages/cli/src/lib/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ export default ({
};
cliReporter(`\n${JSON.stringify(metadata, null, 2)}`);
}

if (exit) {
process.exit(1);
}
}
};
};
11 changes: 11 additions & 0 deletions packages/cli/src/testutils/simple-clean.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h1>This is a document.</h1>
</body>
</html>

0 comments on commit bfa2328

Please sign in to comment.