From 1d17f9bb81916ce9384b631ca6e94d92920a93f1 Mon Sep 17 00:00:00 2001
From: David Anson <david@dlaa.me>
Date: Sat, 18 Jan 2025 21:09:54 -0800
Subject: [PATCH] Add tests for custom rules (markdown-it and micromark) with
 no errors and errors.

---
 .github/workflows/test.yml                    | 34 ++++++++++++++++---
 .../custom-rules-fail.markdownlint-cli2.jsonc | 14 ++++++++
 .../custom-rules-pass.markdownlint-cli2.jsonc |  6 ++++
 test/custom-rule-markdown-it.cjs              | 14 ++++++++
 test/custom-rule-micromark.cjs                | 14 ++++++++
 5 files changed, 77 insertions(+), 5 deletions(-)
 create mode 100644 config/custom-rules-fail.markdownlint-cli2.jsonc
 create mode 100644 config/custom-rules-pass.markdownlint-cli2.jsonc
 create mode 100644 test/custom-rule-markdown-it.cjs
 create mode 100644 test/custom-rule-micromark.cjs

diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 9f729b2..469d29d 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -51,7 +51,7 @@ jobs:
       with:
         globs: |
           *.md
-          test/*
+          test/*.md
       continue-on-error: true
       id: test
     - run: exit 1
@@ -63,7 +63,7 @@ jobs:
     - uses: actions/checkout@v4
     - uses: ./
       with:
-        globs: '*.md,test/*'
+        globs: '*.md,test/*.md'
         separator: ','
       continue-on-error: true
       id: test
@@ -78,7 +78,7 @@ jobs:
     - uses: ./
       with:
         config: 'config/test.markdownlint.jsonc'
-        globs: 'test/*'
+        globs: 'test/*.md'
       continue-on-error: true
       id: test
     - run: exit 1
@@ -91,7 +91,7 @@ jobs:
     - uses: ./
       with:
         config: 'invalid.markdownlint.jsonc'
-        globs: 'test/*'
+        globs: 'test/*.md'
       continue-on-error: true
       id: test
     - run: exit 1
@@ -104,4 +104,28 @@ jobs:
     - uses: ./
       with:
         fix: true
-        globs: 'test/*'
+        globs: 'test/*.md'
+  rules-pass:
+    name: custom rules (README.md, 0 errors)
+    runs-on: ubuntu-latest
+    steps:
+    - uses: actions/checkout@v4
+    - run: rm .markdownlint.json
+    - uses: ./
+      with:
+        config: 'config/custom-rules-pass.markdownlint-cli2.jsonc'
+        globs: 'README.md'
+  rules-fail:
+    name: custom rules (README.md, 2 errors)
+    runs-on: ubuntu-latest
+    steps:
+    - uses: actions/checkout@v4
+    - run: rm .markdownlint.json
+    - uses: ./
+      with:
+        config: 'config/custom-rules-fail.markdownlint-cli2.jsonc'
+        globs: 'README.md'
+      continue-on-error: true
+      id: test
+    - run: exit 1
+      if: steps.test.outcome != 'failure'
diff --git a/config/custom-rules-fail.markdownlint-cli2.jsonc b/config/custom-rules-fail.markdownlint-cli2.jsonc
new file mode 100644
index 0000000..e72c917
--- /dev/null
+++ b/config/custom-rules-fail.markdownlint-cli2.jsonc
@@ -0,0 +1,14 @@
+{
+  "config": {
+    "custom-rule-markdown-it": {
+      "error": true
+    },
+    "custom-rule-micromark": {
+      "error": true
+    }
+  },
+  "customRules": [
+    "../test/custom-rule-markdown-it.cjs",
+    "../test/custom-rule-micromark.cjs"
+  ]
+}
diff --git a/config/custom-rules-pass.markdownlint-cli2.jsonc b/config/custom-rules-pass.markdownlint-cli2.jsonc
new file mode 100644
index 0000000..54acb37
--- /dev/null
+++ b/config/custom-rules-pass.markdownlint-cli2.jsonc
@@ -0,0 +1,6 @@
+{
+  "customRules": [
+    "../test/custom-rule-markdown-it.cjs",
+    "../test/custom-rule-micromark.cjs"
+  ]
+}
diff --git a/test/custom-rule-markdown-it.cjs b/test/custom-rule-markdown-it.cjs
new file mode 100644
index 0000000..0dbe11f
--- /dev/null
+++ b/test/custom-rule-markdown-it.cjs
@@ -0,0 +1,14 @@
+// @ts-check
+
+/** @type {import("markdownlint").Rule} */
+module.exports = {
+  "names": [ "custom-rule-markdown-it" ],
+  "description": "Custom rule using markdown-it",
+  "tags": [ "test" ],
+  "parser": "markdownit",
+  "function": (params, onError) => {
+    if (params.config.error) {
+      onError({ "lineNumber": 1 });
+    }
+  }
+}
diff --git a/test/custom-rule-micromark.cjs b/test/custom-rule-micromark.cjs
new file mode 100644
index 0000000..6ee1a8d
--- /dev/null
+++ b/test/custom-rule-micromark.cjs
@@ -0,0 +1,14 @@
+// @ts-check
+
+/** @type {import("markdownlint").Rule} */
+module.exports = {
+  "names": [ "custom-rule-micromark" ],
+  "description": "Custom rule using micromark",
+  "tags": [ "test" ],
+  "parser": "micromark",
+  "function": (params, onError) => {
+    if (params.config.error) {
+      onError({ "lineNumber": 1 });
+    }
+  }
+}