-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for yaml config file (#105)
* Add support for yaml config file * add documentation
- Loading branch information
Showing
9 changed files
with
182 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
files: | ||
- 'test1/*.test.js' | ||
- 'test2/**/*.test.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
reporters: | ||
- './reporter.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
reporters: | ||
- spec | ||
- '@reporters/silent' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { cwd } from 'node:process' | ||
import { open, readFile } from 'node:fs/promises' | ||
import { join } from 'node:path' | ||
import YAML from 'yaml' | ||
|
||
async function readYamlFile () { | ||
let target | ||
let fd | ||
if (process.env.BORP_CONF_FILE) { | ||
target = process.env.BORP_CONF_FILE | ||
try { | ||
fd = await open(target, 'r') | ||
} catch { | ||
return | ||
} | ||
} else { | ||
const CWD = cwd() | ||
try { | ||
target = join(CWD, '.borp.yaml') | ||
fd = await open(target, 'r') | ||
} catch { | ||
target = join(CWD, '.borp.yml') | ||
try { | ||
fd = await open(target, 'r') | ||
} catch { | ||
// Neither file is available. If we had an application logger that writes | ||
// to stderr, we'd log an error message. But, as it is, we will just | ||
// assume that all errors are "file does not exist."" | ||
return | ||
} | ||
} | ||
} | ||
|
||
let fileData | ||
try { | ||
fileData = await readFile(fd, { encoding: 'utf8' }) | ||
} catch { | ||
// Same thing as noted above. Skip it. | ||
return | ||
} finally { | ||
await fd.close() | ||
} | ||
|
||
return fileData | ||
} | ||
|
||
async function loadConfig () { | ||
const result = [] | ||
const fileData = await readYamlFile() | ||
if (typeof fileData !== 'string') { | ||
return result | ||
} | ||
|
||
let options | ||
try { | ||
options = YAML.parse(fileData) | ||
} catch { | ||
// We just don't care. | ||
return result | ||
} | ||
|
||
if (options.reporters) { | ||
for (const reporter of options.reporters) { | ||
result.push('--reporter') | ||
result.push(reporter) | ||
} | ||
} | ||
|
||
// Append files AFTER all other supported config keys. The runner expects | ||
// them as positional parameters. | ||
if (options.files) { | ||
for (const file of options.files) { | ||
result.push(file) | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
export default loadConfig |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { test } from 'node:test' | ||
import { execa } from 'execa' | ||
import { join } from 'desm' | ||
import { strictEqual } from 'node:assert' | ||
import path from 'node:path' | ||
|
||
const borp = join(import.meta.url, '..', 'borp.js') | ||
const confFilesDir = join(import.meta.url, '..', 'fixtures', 'conf') | ||
|
||
test('reporter from node_modules', async () => { | ||
const cwd = join(import.meta.url, '..', 'fixtures', 'ts-esm') | ||
const { stdout } = await execa('node', [borp], { | ||
cwd, | ||
env: { | ||
BORP_CONF_FILE: path.join(confFilesDir, 'reporters.yaml') | ||
} | ||
}) | ||
|
||
strictEqual(stdout.indexOf('tests 2') >= 0, true) | ||
}) | ||
|
||
test('reporter from relative path', async () => { | ||
const cwd = join(import.meta.url, '..', 'fixtures', 'relative-reporter') | ||
const { stdout } = await execa('node', [borp], { | ||
cwd, | ||
env: { | ||
BORP_CONF_FILE: path.join(confFilesDir, 'relative-reporter.yaml') | ||
} | ||
}) | ||
|
||
strictEqual(/passed:.+add\.test\.js/.test(stdout), true) | ||
strictEqual(/passed:.+add2\.test\.js/.test(stdout), true) | ||
}) | ||
|
||
test('interprets globs for files', async () => { | ||
const cwd = join(import.meta.url, '..', 'fixtures', 'files-glob') | ||
const { stdout } = await execa('node', [borp], { | ||
cwd, | ||
env: { | ||
BORP_CONF_FILE: path.join(confFilesDir, 'glob-files.yaml') | ||
} | ||
}) | ||
|
||
strictEqual(stdout.indexOf('tests 2') >= 0, true) | ||
}) |