1 | #!/usr/bin/env node
|
2 | 'use strict'
|
3 | const loadFromDir = require('tacks/load-from-dir.js')
|
4 |
|
5 | process.exit(main(process.argv.slice(2)))
|
6 |
|
7 | function 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 |
|
18 | function indent (ind, str) {
|
19 | return str.replace(/\n/g, '\n' + ind)
|
20 | }
|
21 |
|
22 | function generateFromDir (dir) {
|
23 | const tacks = loadFromDir(dir)
|
24 | return `'use strict'
|
25 | const path = require('path')
|
26 | const test = require('tap').test
|
27 | const mr = require('npm-registry-mock')
|
28 | const Tacks = require('tacks')
|
29 | const File = Tacks.File
|
30 | const Symlink = Tacks.Symlink
|
31 | const Dir = Tacks.Dir
|
32 | const common = require('../common-tap.js')
|
33 |
|
34 | const basedir = path.join(__dirname, path.basename(__filename, '.js'))
|
35 | const testdir = path.join(basedir, 'testdir')
|
36 | const cachedir = path.join(basedir, 'cache')
|
37 | const globaldir = path.join(basedir, 'global')
|
38 | const tmpdir = path.join(basedir, 'tmp')
|
39 |
|
40 | const 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 |
|
51 | let server
|
52 | const fixture = new Tacks(Dir({
|
53 | cache: Dir(),
|
54 | global: Dir(),
|
55 | tmp: Dir(),
|
56 | testdir: ${indent(' ', tacks.fixture.toSource())}
|
57 | }))
|
58 |
|
59 | function setup () {
|
60 | cleanup()
|
61 | fixture.create(basedir)
|
62 | }
|
63 |
|
64 | function cleanup () {
|
65 | fixture.remove(basedir)
|
66 | }
|
67 |
|
68 | test('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 |
|
77 | test('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 |
|
88 | test('cleanup', function (t) {
|
89 | server.close()
|
90 | cleanup()
|
91 | t.done()
|
92 | })\n`
|
93 | }
|