You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* eslint-env mocha */
  2. const Promise = require('bluebird')
  3. const expect = require('chai').expect
  4. const path = require('path')
  5. const getPackageInfo = require('../src/index')
  6. const readFile = Promise.promisify(require('fs').readFile)
  7. // Test to see if given source actually represents the source
  8. const testSource = (prop, source) => {
  9. return readFile(source.src, 'utf-8')
  10. .then(JSON.parse)
  11. .then((pkg) => expect(pkg).to.deep.equal(source.pkg))
  12. }
  13. describe('get-package-info', () => {
  14. it('should reject promise for non-array non-string props', (done) => {
  15. getPackageInfo(
  16. {},
  17. path.join(__dirname, 'node_modules/we/need/to/go/deeper/')
  18. )
  19. .catch(() => {
  20. done()
  21. })
  22. })
  23. it('should return an empty result', () => {
  24. return getPackageInfo(
  25. [],
  26. path.join(__dirname, 'node_modules/we/need/to/go/deeper/')
  27. )
  28. .then((result) => {
  29. expect(result.values).to.deep.equal({})
  30. expect(result.source).to.deep.equal({})
  31. })
  32. })
  33. it('should return the right properties', () => {
  34. return getPackageInfo(
  35. [
  36. ['productName', 'name'],
  37. 'version',
  38. 'dependencies.some-dependency',
  39. 'devDependencies.some-dev-dependency'
  40. ],
  41. path.join(__dirname, 'node_modules/we/need/to/go/deeper/')
  42. )
  43. .then((result) => {
  44. expect(result.values).to.deep.equal({
  45. productName: 'Deeper',
  46. name: 'Deeper',
  47. version: '1.2.3',
  48. 'dependencies.some-dependency': '~1.2.3',
  49. 'devDependencies.some-dev-dependency': '~1.2.3'
  50. })
  51. return Promise.all(Object.keys(result.source).map(
  52. (prop) => testSource(prop, result.source[prop])
  53. ))
  54. })
  55. })
  56. it('should return the right properties to a given callback', (done) => {
  57. getPackageInfo(
  58. [
  59. ['productName', 'name'],
  60. 'version',
  61. 'dependencies.some-dependency',
  62. 'devDependencies.some-dev-dependency'
  63. ],
  64. path.join(__dirname, 'node_modules/we/need/to/go/deeper/'),
  65. (err, result) => {
  66. expect(err).to.be.null
  67. expect(result.values).to.deep.equal({
  68. productName: 'Deeper',
  69. name: 'Deeper',
  70. version: '1.2.3',
  71. 'dependencies.some-dependency': '~1.2.3',
  72. 'devDependencies.some-dev-dependency': '~1.2.3'
  73. })
  74. // Test source prop points to the prop the value came from
  75. expect(result.source['productName'].prop).to.equal('productName')
  76. expect(result.source['name'].prop).to.equal('productName')
  77. expect(result.source['version'].prop).to.equal('version')
  78. Promise.all(Object.keys(result.source).map(
  79. (prop) => testSource(prop, result.source[prop])
  80. ))
  81. .then(() => done())
  82. }
  83. )
  84. })
  85. it('should resolve with error message when unable to find all props', () => {
  86. return getPackageInfo(
  87. [
  88. ['productName', 'name'],
  89. 'nonexistent',
  90. 'version',
  91. ['this', 'doesntexist']
  92. ],
  93. path.join(__dirname, 'node_modules/we/need/to/go/deeper/')
  94. )
  95. .then(() => {
  96. throw new Error('Should not resolve when props are missing')
  97. })
  98. .catch((err) => {
  99. expect(err.missingProps).to.deep.equal(['nonexistent', ['this', 'doesntexist']])
  100. return Promise.all(Object.keys(err.result.source).map(
  101. (prop) => testSource(prop, err.result.source[prop])
  102. ))
  103. })
  104. })
  105. })