UNPKG

2.03 kBPlain TextView Raw
1#!/usr/bin/env node
2'use strict'
3const loadFromDir = require('tacks/load-from-dir.js')
4
5process.exit(main(process.argv.slice(2)))
6
7function main (argv) {
8 if (argv.length !== 1) {
9 console.error('Usage: maketest <fixturedir>')
10 return 1
11 }
12 const fixturedir = process.argv[2]
13
14 console.log(generateFromDir(fixturedir))
15 return 0
16}
17
18function indent (ind, str) {
19 return str.replace(/\n/g, '\n' + ind)
20}
21
22function generateFromDir (dir) {
23 const tacks = loadFromDir(dir)
24 return `'use strict'
25const path = require('path')
26const test = require('tap').test
27const mr = require('npm-registry-mock')
28const Tacks = require('tacks')
29const File = Tacks.File
30const Symlink = Tacks.Symlink
31const Dir = Tacks.Dir
32const common = require('../common-tap.js')
33
34const basedir = path.join(__dirname, path.basename(__filename, '.js'))
35const testdir = path.join(basedir, 'testdir')
36const cachedir = path.join(basedir, 'cache')
37const globaldir = path.join(basedir, 'global')
38const tmpdir = path.join(basedir, 'tmp')
39
40const conf = {
41 cwd: testdir,
42 env: Object.assign({}, process.env, {
43 npm_config_cache: cachedir,
44 npm_config_tmp: tmpdir,
45 npm_config_prefix: globaldir,
46 npm_config_registry: common.registry,
47 npm_config_loglevel: 'warn'
48 })
49}
50
51let server
52const fixture = new Tacks(Dir({
53 cache: Dir(),
54 global: Dir(),
55 tmp: Dir(),
56 testdir: ${indent(' ', tacks.fixture.toSource())}
57}))
58
59function setup () {
60 cleanup()
61 fixture.create(basedir)
62}
63
64function cleanup () {
65 fixture.remove(basedir)
66}
67
68test('setup', function (t) {
69 setup()
70 mr({port: common.port, throwOnUnmatched: true}, function (err, s) {
71 if (err) throw err
72 server = s
73 t.done()
74 })
75})
76
77test('example', function (t) {
78 common.npm(['install'], conf, function (err, code, stdout, stderr) {
79 if (err) throw err
80 t.is(code, 0, 'command ran ok')
81 t.comment(stdout.trim())
82 t.comment(stderr.trim())
83 // your assertions here
84 t.done()
85 })
86})
87
88test('cleanup', function (t) {
89 server.close()
90 cleanup()
91 t.done()
92})\n`
93}