Skip to content

Commit

Permalink
Add basic api test
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Nov 1, 2020
1 parent d67f191 commit 6867517
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.md text eol=lf
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -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",
Expand Down
76 changes: 76 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
@@ -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')
}
4 changes: 2 additions & 2 deletions test.js → test/dependents.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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')
Expand Down
30 changes: 30 additions & 0 deletions test/fixture/00-various-input.md
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions test/fixture/00-various-output.md
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6867517

Please sign in to comment.