UNPKG

2.2 kBJavaScriptView Raw
1/* @flow */
2
3import 'babel-polyfill'
4import path from 'path'
5import readdir from 'readdir-enhanced'
6import childProcess from 'child_process'
7
8import { DiCy, File } from '../src/main'
9import { cloneFixtures, customMatchers } from './helpers'
10
11const ASYNC_TIMEOUT = 50000
12
13function doCheck (command: string): Promise<boolean> {
14 return new Promise(resolve => {
15 childProcess.exec(command, error => resolve(!error))
16 })
17}
18
19describe('DiCy', () => {
20 let dicy: DiCy
21 let fixturesPath: string
22 const testPath: string = path.join(__dirname, 'fixtures', 'builder-tests')
23 let tests: Array<string> = readdir.sync(testPath, { filter: /\.(lhs|tex|Rnw|lagda)$/i })
24
25 beforeEach(async (done) => {
26 fixturesPath = await cloneFixtures()
27 // $FlowIgnore
28 jasmine.addMatchers(customMatchers)
29 done()
30 })
31
32 describe('can successfully build', () => {
33 for (const name of tests) {
34 const spec = it(name, async (done) => {
35 let expected = { types: [], events: [] }
36 let events = []
37 const filePath = path.resolve(fixturesPath, 'builder-tests', name)
38
39 // Initialize dicy and listen for messages
40 dicy = await DiCy.create(filePath)
41 dicy.state.env.HOME = fixturesPath
42
43 // Load the event archive
44 const eventFilePath = dicy.resolvePath('$DIR/$NAME-events.yaml')
45 if (await File.canRead(eventFilePath)) {
46 expected = await File.safeLoad(eventFilePath)
47 for (const type of expected.types) {
48 dicy.on(type, event => { events.push(event) })
49 }
50 }
51
52 // Run the builder
53 expect(await dicy.run('load')).toBeTruthy()
54
55 for (const command of dicy.options.check || []) {
56 if (!await doCheck(command)) {
57 // $FlowIgnore
58 spec.pend(`Skipped test since required program is not available (\`${command}\` was not successful).`)
59 done()
60 return
61 }
62 }
63
64 expect(await dicy.run('build', 'log', 'save')).toBeTruthy()
65
66 // $FlowIgnore
67 if (expected.types.length !== 0) expect(events).toReceiveEvents(expected.events)
68
69 done()
70 // $FlowIgnore
71 }, ASYNC_TIMEOUT)
72 }
73 })
74})