maketest 1.8 KB

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