Skip to content

Commit

Permalink
Rename option to dryRun for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilafian committed Apr 27, 2024
1 parent b847207 commit 3aedfdb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Command-line flags:
| ----------------- | ------------------------------------------------------------------- | ---------- |
| `--continue` | Report messages but do not throw an error if validation failed. | N/A |
| `--delay` | Debounce pause in milliseconds between each file validation. | **number** |
| `--dry-run` | Bypass validation (for usage while building your CI). | N/A |
| `--exclude` | Comma separated list of strings to match in paths to skip. | **string** |
| `--ignore` | Skip validation messages containing a string or matching a regex. | **string** |
| `--ignore-config` | File containing strings and regexes of validation messages to skip. | **string** |
Expand Down Expand Up @@ -136,12 +137,12 @@ $ node examples.js
| Name (key) | Type | Default | Description |
| :--------------- | :---------------------- | :------------------------------- | :------------------------------------------------------ |
| `checkUrl` | **string** | `'https://validator.w3.org/nu/'` | W3C validation API endpoint. |
| `dryRun` | **boolean** | `false` | Bypass validation (for usage while building your CI). |
| `filename` | **string** | `null` | HTML file to validate. |
| `html` | **string** | `null` | HTML string to validate. |
| `ignoreLevel` | `'info'` or `'warning'` | `null` | Skip unwanted messages.* |
| `ignoreMessages` | **array** | `[]` | Skip messages containing a string or matching a regex.* |
| `output` | `'json'` or `'html'` | `'json'` | Get results as an array or as a web page. |
| `skip` | **boolean** | `false` | bypass validation (for usage while building your CI). |
| `website` | **string** | `null` | URL of website to validate. |

*The `ignoreMessages` and `ignoreLevel` options only work for `'json'` output. 
Expand Down Expand Up @@ -172,7 +173,7 @@ type ValidatorResults = {
status: number,
messages: ValidatorResultsMessage[] | null, //for 'json' output
display: string | null, //for 'html' output
skip: boolean,
dryRun: boolean,
};
```

Expand Down
10 changes: 5 additions & 5 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ import fs from 'fs';
import slash from 'slash';

// Parameters and flags
const validFlags = ['continue', 'delay', 'exclude', 'ignore', 'ignore-config', 'note', 'quiet', 'trim'];
const validFlags = ['continue', 'delay', 'dry-run', 'exclude', 'ignore', 'ignore-config', 'note', 'quiet', 'trim'];
const cli = cliArgvUtil.parse(validFlags);
const files = cli.params;
const ignore = cli.flagMap.ignore ?? null;
const ignoreConfig = cli.flagMap.ignoreConfig ?? null;
const delay = Number(cli.flagMap.delay) || 500; //default half second debounce pause
const trim = Number(cli.flagMap.trim) || null;
const skip = process.env.w3cHtmlValidator === 'skip'; //bash: export w3cHtmlValidator=skip
const dryRunMode = cli.flagOn.dryRun || process.env.w3cHtmlValidator === 'dry-run'; //bash: export w3cHtmlValidator=dry-run

// Validator
const globOptions = { ignore: '**/node_modules/**/*' };
Expand All @@ -55,8 +55,8 @@ const error =
null;
if (error)
throw Error('[w3c-html-validator] ' + error);
if (skip)
w3cHtmlValidator.skipNotice();
if (dryRunMode)
w3cHtmlValidator.dryRunNotice();
if (filenames.length > 1 && !cli.flagOn.quiet)
w3cHtmlValidator.summary(filenames.length);
const reporterOptions = {
Expand All @@ -74,7 +74,7 @@ const getIgnoreMessages = () => {
const isRegex = /^\/.*\/$/; //starts and ends with a slash indicating it's a regex
return rawLines.map(line => isRegex.test(line) ? new RegExp(line.slice(1, -1)) : line);
};
const baseOptions = { ignoreMessages: getIgnoreMessages(), skip: skip };
const baseOptions = { ignoreMessages: getIgnoreMessages(), dryRun: dryRunMode };
const options = (filename) => ({ filename: filename, ...baseOptions });
const handleResults = (results) => w3cHtmlValidator.reporter(results, reporterOptions);
const getReport = (filename) => w3cHtmlValidator.validate(options(filename)).then(handleResults);
Expand Down
28 changes: 14 additions & 14 deletions spec/mocha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ describe('Library module', () => {
assertDeepStrictEqual(actual, expected);
});

it('has functions named validate(), skipNotice() summary(), and reporter()', () => {
it('has functions named validate(), dryRunNotice() summary(), and reporter()', () => {
const module = w3cHtmlValidator;
const actual = Object.keys(module).sort().map(key => [key, typeof module[key]]);
const expected = [
['reporter', 'function'],
['skipNotice', 'function'],
['summary', 'function'],
['validate', 'function'],
['version', 'string'],
['dryRunNotice', 'function'],
['reporter', 'function'],
['summary', 'function'],
['validate', 'function'],
['version', 'string'],
];
assertDeepStrictEqual(actual, expected);
});
Expand All @@ -81,7 +81,7 @@ describe('Pretty-Print JSON website', () => {
status: 200,
messages: [],
display: null,
skip: false,
dryRun: false,
};
assertDeepStrictEqual(actual, expected, done);
};
Expand All @@ -107,7 +107,7 @@ describe('Valid HTML string', () => {
status: 200,
messages: [],
display: null,
skip: false,
dryRun: false,
};
assertDeepStrictEqual(actual, expected, done);
};
Expand All @@ -128,7 +128,7 @@ describe('Valid HTML string', () => {
output: 'html',
status: 200,
messages: null,
skip: false,
dryRun: false,
};
assertDeepStrictEqual(actual, expected, done);
};
Expand Down Expand Up @@ -180,7 +180,7 @@ describe('Invalid HTML string', () => {
},
],
display: null,
skip: false,
dryRun: false,
};
assertDeepStrictEqual(actual, expected, done);
};
Expand All @@ -201,7 +201,7 @@ describe('Invalid HTML string', () => {
output: 'html',
status: 200,
messages: null,
skip: false,
dryRun: false,
};
assertDeepStrictEqual(actual, expected, done);
};
Expand All @@ -227,7 +227,7 @@ describe('HTML file', () => {
status: 200,
messages: [],
display: null,
skip: false,
dryRun: false,
};
assertDeepStrictEqual(actual, expected, done);
};
Expand Down Expand Up @@ -358,7 +358,7 @@ describe('Network request failure', () => {
message: '503 Service Unavailable https://centerkey.com/rest/status/503/?out=json',
}],
display: null,
skip: false,
dryRun: false,
};
assertDeepStrictEqual(actual, expected, done);
};
Expand Down Expand Up @@ -389,7 +389,7 @@ describe('The reporter() function', () => {
status: 200,
messages: [],
display: null,
skip: false,
dryRun: false,
};
assertDeepStrictEqual(actual, expected, done);
};
Expand Down
16 changes: 8 additions & 8 deletions w3c-html-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type ValidatorSettings = {
ignoreLevel: 'info' | 'warning', //skip unwanted validation messages ('warning' also skips 'info')
ignoreMessages: (string | RegExp)[], //patterns to skip unwanted validation messages
output: ValidatorResultsOutput, //'json' or 'html'
skip: boolean, //bypass validation (for usage while building your CI)
dryRun: boolean, //bypass validation (for usage while building your CI)
};
export type ValidatorResultsMessage = {
// type subType
Expand Down Expand Up @@ -48,7 +48,7 @@ export type ValidatorResults = {
status: number,
messages: ValidatorResultsMessage[] | null,
display: string | null,
skip: boolean,
dryRun: boolean,
};
export type ValidatorResultsOutput = ValidatorResults['output'];
export type ReporterSettings = {
Expand All @@ -66,10 +66,10 @@ const w3cHtmlValidator = {
validate(options: Partial<ValidatorSettings>): Promise<ValidatorResults> {
const defaults = {
checkUrl: 'https://validator.w3.org/nu/',
dryRun: false,
ignoreLevel: null,
ignoreMessages: [],
output: 'json',
skip: false,
};
const settings = { ...defaults, ...options };
if (!settings.html && !settings.filename && !settings.website)
Expand Down Expand Up @@ -122,7 +122,7 @@ const w3cHtmlValidator = {
status: response.statusCode || -1,
messages: json ? response.body.messages : null,
display: json ? null : response.text,
skip: settings.skip,
dryRun: settings.dryRun,
});
type ReasonResponse = { request: { url: string }, res: { statusMessage: string }};
type ReasonError = Error & { errno: number, response: request.Response & ReasonResponse };
Expand All @@ -136,17 +136,17 @@ const w3cHtmlValidator = {
const pseudoResponse = <request.Response>{
statusCode: 200,
body: { messages: [] },
text: 'Validation skipped.',
text: 'Validation bypassed.',
};
const pseudoRequest = (): Promise<request.Response> =>
new Promise(resolve => resolve(pseudoResponse));
const validation = settings.skip ? pseudoRequest() : w3cRequest;
const validation = settings.dryRun ? pseudoRequest() : w3cRequest;
return validation.then(filterMessages).then(toValidatorResults).catch(handleError);
},

skipNotice() {
dryRunNotice() {
log(chalk.gray('w3c-html-validator'),
chalk.yellowBright('skip mode:'), chalk.whiteBright('validation being bypassed'));
chalk.yellowBright('dry run mode:'), chalk.whiteBright('validation being bypassed'));
},

summary(numFiles: number) {
Expand Down

0 comments on commit 3aedfdb

Please sign in to comment.