test.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. var n = 'rc'+Math.random()
  2. var assert = require('assert')
  3. process.env[n+'_envOption'] = 42
  4. var config = require('../')(n, {
  5. option: true
  6. })
  7. console.log(config)
  8. assert.equal(config.option, true)
  9. assert.equal(config.envOption, 42)
  10. var customArgv = require('../')(n, {
  11. option: true
  12. }, { // nopt-like argv
  13. option: false,
  14. envOption: 24,
  15. argv: {
  16. remain: [],
  17. cooked: ['--no-option', '--envOption', '24'],
  18. original: ['--no-option', '--envOption=24']
  19. }
  20. })
  21. console.log(customArgv)
  22. assert.equal(customArgv.option, false)
  23. assert.equal(customArgv.envOption, 24)
  24. var fs = require('fs')
  25. var path = require('path')
  26. var jsonrc = path.resolve('.' + n + 'rc');
  27. fs.writeFileSync(jsonrc, [
  28. '{',
  29. '// json overrides default',
  30. '"option": false,',
  31. '/* env overrides json */',
  32. '"envOption": 24',
  33. '}'
  34. ].join('\n'));
  35. var commentedJSON = require('../')(n, {
  36. option: true
  37. })
  38. fs.unlinkSync(jsonrc);
  39. console.log(commentedJSON)
  40. assert.equal(commentedJSON.option, false)
  41. assert.equal(commentedJSON.envOption, 42)
  42. assert.equal(commentedJSON.config, jsonrc)
  43. assert.equal(commentedJSON.configs.length, 1)
  44. assert.equal(commentedJSON.configs[0], jsonrc)