From 6867517ff6b7506a6d51601500727489728fdfaf Mon Sep 17 00:00:00 2001
From: Vincent Weevers <mail@vincentweevers.nl>
Date: Sun, 1 Nov 2020 09:44:40 +0100
Subject: [PATCH] Add basic api test

---
 .gitattributes                    |  1 +
 package.json                      |  5 +-
 test/api.js                       | 76 +++++++++++++++++++++++++++++++
 test.js => test/dependents.js     |  4 +-
 test/fixture/00-various-input.md  | 30 ++++++++++++
 test/fixture/00-various-output.md | 32 +++++++++++++
 6 files changed, 144 insertions(+), 4 deletions(-)
 create mode 100644 .gitattributes
 create mode 100644 test/api.js
 rename test.js => test/dependents.js (94%)
 create mode 100644 test/fixture/00-various-input.md
 create mode 100644 test/fixture/00-various-output.md

diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..1fe2478
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1 @@
+*.md text eol=lf
diff --git a/package.json b/package.json
index 32e7040..0dd79b1 100644
--- a/package.json
+++ b/package.json
@@ -7,7 +7,7 @@
   "bin": "cli.js",
   "main": "index.js",
   "scripts": {
-    "test": "standard && depcheck --ignores level-community && node cli.js && node test.js",
+    "test": "standard && depcheck --ignores level-community && node cli.js && tape test/*.js",
     "hallmark": "node cli.js --fix",
     "check-licenses": "npm-consider install --test --production"
   },
@@ -62,7 +62,8 @@
     "npm-consider": "^1.7.0",
     "rimraf": "^3.0.0",
     "standard": "^15.0.0",
-    "tape": "^5.0.1"
+    "tape": "^5.0.1",
+    "tempy": "0.2.1"
   },
   "repository": {
     "type": "git",
diff --git a/test/api.js b/test/api.js
new file mode 100644
index 0000000..259c2da
--- /dev/null
+++ b/test/api.js
@@ -0,0 +1,76 @@
+'use strict'
+
+const test = require('tape')
+const fs = require('fs')
+const path = require('path')
+const tempy = require('tempy') // Locked to 0.2.1 for node 6 support
+const execFileSync = require('child_process').execFileSync
+const hallmark = require('..')
+
+test('lints various', function (t) {
+  run('00-various-input', '00-various-input', 'lint', {}, (err, { file, actual, expected }) => {
+    t.ifError(err)
+    t.is(actual, expected)
+    t.same(file.messages.map(String), [
+      'test.md:5:3-5:6: Found reference to undefined definition',
+      'test.md:6:3-6:21: Don’t use literal URLs without angle brackets',
+      'test.md:16:1-16:9: Code blocks should be fenced', // TODO: sort messages by line
+      'test.md:12:23: Cell should be padded',
+      'test.md:28:4-28:5: Checked checkboxes should use `x` as a marker'
+    ])
+    t.end()
+  })
+})
+
+test('fixes various', function (t) {
+  run('00-various-input', '00-various-output', 'fix', {}, (err, { file, actual, expected }) => {
+    t.ifError(err)
+    t.is(actual, expected)
+    t.same(file.messages.map(String), [
+      // This can't be fixed automatically
+      'test.md:5:3-5:6: Found reference to undefined definition'
+    ])
+    t.end()
+  })
+})
+
+function run (inputFixture, outputFixture, method, opts, test) {
+  const cwd = tempy.directory()
+  const inputFile = path.join(__dirname, 'fixture', inputFixture + '.md')
+  const outputFile = path.join(__dirname, 'fixture', outputFixture + '.md')
+  const pkgFile = path.join(cwd, 'package.json')
+  const mdFile = path.join(cwd, 'test.md')
+  const options = opts.options || {}
+  const stdio = 'ignore'
+
+  const pkg = {
+    name: 'test',
+    version: '0.0.0',
+    repository: 'https://github.com/test/test.git',
+    private: true
+  }
+
+  execFileSync('git', ['init', '.'], { cwd, stdio })
+
+  const input = readNormal(inputFile)
+  const expected = readNormal(outputFile)
+
+  fs.writeFileSync(pkgFile, JSON.stringify(pkg))
+  fs.writeFileSync(mdFile, input)
+
+  options.cwd = cwd
+  options.files = ['test.md']
+
+  hallmark[method](options, function (err, result) {
+    if (err) return test(err)
+
+    const file = result && result.files[0]
+    const actual = readNormal(mdFile)
+
+    test(err, { file, cwd, actual, expected })
+  })
+}
+
+function readNormal (fp) {
+  return fs.readFileSync(fp, 'utf8').replace(/\r\n/g, '\n')
+}
diff --git a/test.js b/test/dependents.js
similarity index 94%
rename from test.js
rename to test/dependents.js
index ea46938..b125f9c 100644
--- a/test.js
+++ b/test/dependents.js
@@ -29,7 +29,7 @@ const dependents = [
 ]
 
 for (const repo of dependents) {
-  const cwd = path.join(__dirname, 'dependents', repo.toLowerCase())
+  const cwd = path.resolve(__dirname, '..', 'dependents', repo.toLowerCase())
   const url = `https://github.com/${repo}.git`
 
   test(`smoke test ${repo}`, function (t) {
@@ -38,7 +38,7 @@ for (const repo of dependents) {
 
       // Pipe stdout to stderr because our stdout is for TAP
       const stdio = ['ignore', process.stderr, process.stderr, 'ipc']
-      const cli = path.join(__dirname, 'cli.js')
+      const cli = path.resolve(__dirname, '..', 'cli.js')
 
       cp.fork(cli, { cwd, stdio }).on('exit', function (code) {
         t.is(code, 0, 'hallmark linter exited with code 0')
diff --git a/test/fixture/00-various-input.md b/test/fixture/00-various-input.md
new file mode 100644
index 0000000..b069375
--- /dev/null
+++ b/test/fixture/00-various-input.md
@@ -0,0 +1,30 @@
+# test
+
+* a @user
+* b #12
+* [c]
+* http://example.com
+
+## table
+
+| beep | boop |
+|:-----|:-----|
+| longer content | foo|
+
+## code blocks
+
+    no()
+
+```
+yes()
+```
+
+```js
+yes()
+```
+
+## checkbox
+
+- [X] no
+- [x] yes
+- [ ] foo
diff --git a/test/fixture/00-various-output.md b/test/fixture/00-various-output.md
new file mode 100644
index 0000000..bef4851
--- /dev/null
+++ b/test/fixture/00-various-output.md
@@ -0,0 +1,32 @@
+# test
+
+- a [**@user**](https://github.com/user)
+- b [#12](https://github.com/test/test/issues/12)
+- [c]
+- <http://example.com>
+
+## table
+
+| beep           | boop |
+| :------------- | :--- |
+| longer content | foo  |
+
+## code blocks
+
+```
+no()
+```
+
+```
+yes()
+```
+
+```js
+yes()
+```
+
+## checkbox
+
+- [x] no
+- [x] yes
+- [ ] foo