UNPKG

2.17 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|Pnw)$/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
42 // Load the event archive
43 const eventFilePath = dicy.resolvePath('$ROOTDIR/$NAME-events.yaml')
44 if (await File.canRead(eventFilePath)) {
45 expected = await File.safeLoad(eventFilePath)
46 for (const type of expected.types) {
47 dicy.on(type, event => { events.push(event) })
48 }
49 }
50
51 // Run the builder
52 expect(await dicy.run('load')).toBeTruthy()
53
54 for (const command of dicy.options.check || []) {
55 if (!await doCheck(command)) {
56 // $FlowIgnore
57 spec.pend(`Skipped test since required program is not available (\`${command}\` was not successful).`)
58 done()
59 return
60 }
61 }
62
63 expect(await dicy.run('build', 'log', 'save')).toBeTruthy()
64
65 // $FlowIgnore
66 if (expected.types.length !== 0) expect(events).toReceiveEvents(expected.events)
67
68 done()
69 // $FlowIgnore
70 }, ASYNC_TIMEOUT)
71 }
72 })
73})