Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Jan 4, 2022
1 parent 0cfb7d0 commit 2d718f4
Show file tree
Hide file tree
Showing 21 changed files with 198 additions and 0 deletions.
5 changes: 5 additions & 0 deletions test/config-files-cli2-jsonc-alternate.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dir1/viewme.md:5 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]
dir1/viewme.md:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "# Description"]
dir2/viewme.md:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "# Description"]
viewme.md:5 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]
viewme.md:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "# Description"]
4 changes: 4 additions & 0 deletions test/config-files-cli2-jsonc-alternate.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
markdownlint-cli2-config vX.Y.Z (markdownlint vX.Y.Z)
Finding: **/*.md
Linting: 3 file(s)
Summary: 5 error(s)
1 change: 1 addition & 0 deletions test/config-files-cli2-jsonc-invalid.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
markdownlint-cli2-config vX.Y.Z (markdownlint vX.Y.Z)
5 changes: 5 additions & 0 deletions test/config-files-cli2-jsonc.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dir1/viewme.md:5 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]
dir1/viewme.md:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "# Description"]
dir2/viewme.md:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "# Description"]
viewme.md:5 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]
viewme.md:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "# Description"]
4 changes: 4 additions & 0 deletions test/config-files-cli2-jsonc.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
markdownlint-cli2-config vX.Y.Z (markdownlint vX.Y.Z)
Finding: **/*.md
Linting: 3 file(s)
Summary: 5 error(s)
11 changes: 11 additions & 0 deletions test/config-files/cfg/.markdownlint-cli2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @ts-check

"use strict";

module.exports = {
"config": {
"no-trailing-spaces": false,
"no-multiple-space-atx": false,
"single-trailing-newline": false
}
};
7 changes: 7 additions & 0 deletions test/config-files/cfg/.markdownlint-cli2.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"config": {
"no-trailing-spaces": false,
"no-multiple-space-atx": false,
"single-trailing-newline": false
}
}
4 changes: 4 additions & 0 deletions test/config-files/cfg/.markdownlint-cli2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
config:
no-trailing-spaces: false
no-multiple-space-atx: false
single-trailing-newline: false
9 changes: 9 additions & 0 deletions test/config-files/cfg/.markdownlint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @ts-check

"use strict";

module.exports = {
"no-trailing-spaces": false,
"no-multiple-space-atx": false,
"single-trailing-newline": false
};
5 changes: 5 additions & 0 deletions test/config-files/cfg/.markdownlint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"no-trailing-spaces": false,
"no-multiple-space-atx": false,
"single-trailing-newline": false
}
5 changes: 5 additions & 0 deletions test/config-files/cfg/.markdownlint.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"no-trailing-spaces": false,
"no-multiple-space-atx": false,
"single-trailing-newline": false
}
3 changes: 3 additions & 0 deletions test/config-files/cfg/.markdownlint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
no-trailing-spaces: false
no-multiple-space-atx: false
single-trailing-newline: false
3 changes: 3 additions & 0 deletions test/config-files/cfg/.markdownlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
no-trailing-spaces: false
no-multiple-space-atx: false
single-trailing-newline: false
7 changes: 7 additions & 0 deletions test/config-files/cfg/alternate.markdownlint-cli2.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"config": {
"no-trailing-spaces": false,
"no-multiple-space-atx": false,
"single-trailing-newline": false
}
}
1 change: 1 addition & 0 deletions test/config-files/cfg/invalid.markdownlint-cli2.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{
14 changes: 14 additions & 0 deletions test/config-files/dir1/viewme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Title

> Tagline

# Description

Text text text
Text text text
Text text text

## Summary

Text text text
8 changes: 8 additions & 0 deletions test/config-files/dir2/.markdownlint-cli2.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"config": {
"no-trailing-spaces": false,
"no-multiple-blanks": false,
"no-multiple-space-atx": false,
"single-trailing-newline": false
}
}
14 changes: 14 additions & 0 deletions test/config-files/dir2/viewme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Title

> Tagline

# Description

Text text text
Text text text
Text text text

## Summary

Text text text
14 changes: 14 additions & 0 deletions test/config-files/viewme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Title

> Tagline

# Description

Text text text
Text text text
Text text text

## Summary

Text text text
37 changes: 37 additions & 0 deletions test/markdownlint-cli2-test-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ const testCases =
"usesScript": true
});

testCase({
"name": "one-argument-config",
"script": "markdownlint-cli2-config.js",
"args": [ "../config-files/cfg/.markdownlint-cli2.jsonc" ],
"exitCode": 1,
"cwd": "no-config",
"usesScript": true
});

testCase({
"name": "no-files",
"args": [ "nothing-matches" ],
Expand Down Expand Up @@ -468,6 +477,34 @@ const testCases =
"usesScript": true
});

testCase({
"name": "config-files-cli2-jsonc",
"script": "markdownlint-cli2-config.js",
"args": [ "cfg/.markdownlint-cli2.jsonc", "**/*.md" ],
"exitCode": 1,
"cwd": "config-files",
"usesScript": true
});

testCase({
"name": "config-files-cli2-jsonc-alternate",
"script": "markdownlint-cli2-config.js",
"args": [ "cfg/alternate.markdownlint-cli2.jsonc", "**/*.md" ],
"exitCode": 1,
"cwd": "config-files",
"usesScript": true
});

testCase({
"name": "config-files-cli2-jsonc-invalid",
"script": "markdownlint-cli2-config.js",
"args": [ "cfg/invalid.markdownlint-cli2.jsonc", "**/*.md" ],
"exitCode": 2,
"stderrRe": /Unexpected end of JSON input/u,
"cwd": "config-files",
"usesScript": true
});

testCase({
"name": "customRules",
"args": [ "**/*.md" ],
Expand Down
37 changes: 37 additions & 0 deletions test/one-argument-config.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
markdownlint-cli2-config vX.Y.Z (markdownlint vX.Y.Z)
https://github.com/DavidAnson/markdownlint-cli2

Syntax: markdownlint-cli2 glob0 [glob1] [...] [globN]
markdownlint-cli2-fix glob0 [glob1] [...] [globN]
markdownlint-cli2-config config-file glob0 [glob1] [...] [globN]

Glob expressions (from the globby library):
- * matches any number of characters, but not /
- ? matches a single character, but not /
- ** matches any number of characters, including /
- {} allows for a comma-separated list of "or" expressions
- ! or # at the beginning of a pattern negate the match
- : at the beginning identifies a literal file path

Dot-only glob:
- The command "markdownlint-cli2 ." would lint every file in the current directory tree which is probably not intended
- Instead, it is mapped to "markdownlint-cli2 *.{md,markdown}" which lints all Markdown files in the current directory
- To lint every file in the current directory tree, the command "markdownlint-cli2 **" can be used instead

Configuration via:
- .markdownlint-cli2.jsonc
- .markdownlint-cli2.yaml
- .markdownlint-cli2.js
- .markdownlint.jsonc or .markdownlint.json
- .markdownlint.yaml or .markdownlint.yml
- .markdownlint.js

Cross-platform compatibility:
- UNIX and Windows shells expand globs according to different rules; quoting arguments is recommended
- Some Windows shells don't handle single-quoted (') arguments well; double-quote (") is recommended
- Shells that expand globs do not support negated patterns (!node_modules); quoting is required here
- Some UNIX shells parse exclamation (!) in double-quotes; hashtag (#) is recommended in these cases
- The path separator is forward slash (/) on all platforms; backslash (\) is automatically converted

The most compatible syntax for cross-platform support:
$ markdownlint-cli2 "**/*.md" "#node_modules"

5 comments on commit 2d718f4

@nschonni
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to look at the snapshot testing in Ava https://github.com/avajs/ava/blob/main/docs/04-snapshot-testing.md

@DavidAnson
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nschonni Oooh!! I like that. My recent change to JSON for these tests makes it even easier to apply. (Tho I think the RegExp exception needs to remain.) I’ll play with this after the current set of changes. Thanks!!

@DavidAnson
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nschonni It's a great improvement, thank you for the suggestion! I give up a tiny bit of control, lose the guarantee that each scenario is identical across all three cases, and lock myself into ava a little more - but the overall simplification seems compelling.

@nschonni
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I had been playing with the Jest version in htmlhint/HTMLHint#823
Generally, I found testing CLIs with these tools is generally painful 😆

@DavidAnson
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, the example output section seems empty: https://github.com/htmlhint/HTMLHint#-example-output

Please sign in to comment.