Skip to content

Commit

Permalink
Add dependency tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bradlc committed Jun 1, 2021
1 parent 4d280c8 commit 57a2ef4
Showing 1 changed file with 195 additions and 0 deletions.
195 changes: 195 additions & 0 deletions test/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,198 @@ testCb('--watch does exit on closing stdin (Ctrl-D/EOF)', (t) => {
})
cp.stdin.end()
})

testCb('--watch watches dependencies', (t) => {
let cp

t.plan(2)

ENV('', ['s.css', 'a.css', 'b.css']).then((dir) => {
fs.writeFile(
path.join(dir, 'postcss.config.js'),
`
const fs = require('fs')
module.exports = {
plugins: [
(root, result) => {
const file = '${path.resolve(dir, 'a.css')}'
result.messages.push({
plugin: 'test',
type: 'dependency',
file,
parent: result.opts.from,
})
root.nodes = []
root.append(fs.readFileSync(file, 'utf8'))
return root
}
]
}
`
)
.then(() => {
// Init watcher:
const watcher = chokidar.watch('.', {
cwd: dir,
ignoreInitial: true,
awaitWriteFinish: true,
})

// On the first output:
watcher.on('add', (p) => {
// Assert, then change the source file
if (p === 'output.css') {
isEqual(p, 'test/fixtures/a.css')
.then(() => read('test/fixtures/b.css'))
.then((css) => fs.writeFile(path.join(dir, 'a.css'), css))
.catch(done)
}
})

// When the change is picked up:
watcher.on('change', (p) => {
if (p === 'output.css') {
isEqual(p, 'test/fixtures/b.css')
.then(() => done())
.catch(done)
}
})

// Start postcss-cli:
watcher.on('ready', () => {
// Using exec() and quoting "*.css" to test watch's glob handling:
cp = exec(
`node ${path.resolve(
'bin/postcss'
)} "s.css" -o output.css --no-map -w`,
{ cwd: dir }
)
cp.on('error', t.end)
cp.on('exit', (code) => {
if (code) t.end(code)
})
})

// Helper functions:
function isEqual(p, expected) {
return Promise.all([read(path.join(dir, p)), read(expected)]).then(
([a, e]) => t.is(a, e)
)
}

function done(err) {
try {
cp.kill()
} catch {}

t.end(err)
}
})
.catch(t.end)
})

// Timeout:
setTimeout(() => t.end('test timeout'), 50000)
})

testCb('--watch watches directory dependencies', (t) => {
let cp

t.plan(2)

ENV('', ['s.css', 'base/level-1/b.css', 'base/level-1/level-2/a.css']).then(
(dir) => {
fs.writeFile(
path.join(dir, 'postcss.config.js'),
`
const fs = require('fs')
module.exports = {
plugins: [
(root, result) => {
result.messages.push({
plugin: 'test',
type: 'dir-dependency',
dir: '${path.resolve(dir, 'base')}',
parent: result.opts.from,
})
root.nodes = []
root.append(fs.readFileSync('${path.resolve(
dir,
'base/level-1/level-2/a.css'
)}', 'utf8'))
return root
}
]
}
`
)
.then(() => {
// Init watcher:
const watcher = chokidar.watch('.', {
cwd: dir,
ignoreInitial: true,
awaitWriteFinish: true,
})

// On the first output:
watcher.on('add', (p) => {
// Assert, then change the source file
if (p === 'output.css') {
isEqual(p, 'test/fixtures/base/level-1/level-2/a.css')
.then(() => read('test/fixtures/base/level-1/b.css'))
.then((css) =>
fs.writeFile(
path.join(dir, 'base/level-1/level-2/a.css'),
css
)
)
.catch(done)
}
})

// When the change is picked up:
watcher.on('change', (p) => {
if (p === 'output.css') {
isEqual(p, 'test/fixtures/base/level-1/b.css')
.then(() => done())
.catch(done)
}
})

// Start postcss-cli:
watcher.on('ready', () => {
// Using exec() and quoting "*.css" to test watch's glob handling:
cp = exec(
`node ${path.resolve(
'bin/postcss'
)} "s.css" -o output.css --no-map -w`,
{ cwd: dir }
)
cp.on('error', t.end)
cp.on('exit', (code) => {
if (code) t.end(code)
})
})

// Helper functions:
function isEqual(p, expected) {
return Promise.all([read(path.join(dir, p)), read(expected)]).then(
([a, e]) => t.is(a, e)
)
}

function done(err) {
try {
cp.kill()
} catch {}

t.end(err)
}
})
.catch(t.end)
}
)

// Timeout:
setTimeout(() => t.end('test timeout'), 50000)
})

0 comments on commit 57a2ef4

Please sign in to comment.