ignore.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. 'use strict'
  2. const common = require('../common')
  3. const fs = require('fs-extra')
  4. const ignore = require('../ignore')
  5. const path = require('path')
  6. const packager = require('..')
  7. const test = require('ava')
  8. const util = require('./_util')
  9. function ignoreTest (t, opts, ignorePattern, ignoredFile) {
  10. opts.dir = util.fixtureSubdir('basic')
  11. if (ignorePattern) {
  12. opts.ignore = ignorePattern
  13. }
  14. const targetDir = path.join(t.context.tempDir, 'result')
  15. ignore.generateIgnores(opts)
  16. return fs.copy(opts.dir, targetDir, {
  17. dereference: false,
  18. filter: ignore.userIgnoreFilter(opts)
  19. }).then(() => fs.pathExists(path.join(targetDir, 'package.json')))
  20. .then(exists => {
  21. t.true(exists, 'The expected output directory should exist and contain files')
  22. return fs.pathExists(path.join(targetDir, ignoredFile))
  23. }).then(exists => t.false(exists, `Ignored file '${ignoredFile}' should not exist in copied directory`))
  24. }
  25. function ignoreOutDirTest (t, opts, distPath) {
  26. opts.name = 'ignoreOutDirTest'
  27. opts.dir = t.context.workDir
  28. // we don't use path.join here to avoid normalizing
  29. const outDir = opts.dir + path.sep + distPath
  30. opts.out = outDir
  31. return fs.copy(util.fixtureSubdir('basic'), t.context.workDir, {
  32. dereference: true,
  33. stopOnErr: true,
  34. filter: file => { return path.basename(file) !== 'node_modules' }
  35. }).then(() =>
  36. // create out dir before packager (real world issue - when second run includes unignored out dir)
  37. fs.ensureDir(outDir)
  38. ).then(() =>
  39. // create file to ensure that directory will be not ignored because empty
  40. fs.open(path.join(outDir, 'ignoreMe'), 'w')
  41. ).then(fd => fs.close(fd))
  42. .then(() => packager(opts))
  43. .then(() => fs.pathExists(path.join(outDir, common.generateFinalBasename(opts), util.generateResourcesPath(opts), 'app', path.basename(outDir))))
  44. .then(exists => t.false(exists, 'Out dir must not exist in output app directory'))
  45. }
  46. function ignoreImplicitOutDirTest (t, opts) {
  47. opts.name = 'ignoreImplicitOutDirTest'
  48. opts.dir = t.context.workDir
  49. delete opts.out
  50. const testFilename = 'ignoreMe'
  51. let previousPackedResultDir
  52. return fs.copy(util.fixtureSubdir('basic'), t.context.workDir, {
  53. dereference: true,
  54. stopOnErr: true,
  55. filter: file => { return path.basename(file) !== 'node_modules' }
  56. }).then(() => {
  57. previousPackedResultDir = path.join(opts.dir, `${common.sanitizeAppName(opts.name)}-linux-ia32`)
  58. return fs.ensureDir(previousPackedResultDir)
  59. }).then(() =>
  60. // create file to ensure that directory will be not ignored because empty
  61. fs.open(path.join(previousPackedResultDir, testFilename), 'w')
  62. ).then(fd => fs.close(fd))
  63. .then(() => packager(opts))
  64. .then(() => fs.pathExists(path.join(opts.dir, common.generateFinalBasename(opts), util.generateResourcesPath(opts), 'app', testFilename)))
  65. .then(exists => t.false(exists, 'Out dir must not exist in output app directory'))
  66. }
  67. test('generateIgnores ignores the generated temporary directory only on Linux', t => {
  68. const tmpdir = '/foo/bar'
  69. const expected = path.join(tmpdir, 'electron-packager')
  70. let opts = {tmpdir}
  71. ignore.generateIgnores(opts)
  72. // Array.prototype.includes is added (not behind a feature flag) in Node 6
  73. if (process.platform === 'linux') {
  74. t.false(opts.ignore.indexOf(expected) === -1, 'temporary dir in opts.ignore')
  75. } else {
  76. t.true(opts.ignore.indexOf(expected) === -1, 'temporary dir not in opts.ignore')
  77. }
  78. })
  79. test('generateOutIgnores ignores all possible platform/arch permutations', (t) => {
  80. const ignores = ignore.generateOutIgnores({name: 'test'})
  81. t.is(ignores.length, util.allPlatformArchCombosCount)
  82. })
  83. util.testSinglePlatformParallel('ignore default test: .o files', ignoreTest, null, 'ignore.o')
  84. util.testSinglePlatformParallel('ignore default test: .obj files', ignoreTest, null, 'ignore.obj')
  85. util.testSinglePlatformParallel('ignore test: string in array', ignoreTest, ['ignorethis'],
  86. 'ignorethis.txt')
  87. util.testSinglePlatformParallel('ignore test: string', ignoreTest, 'ignorethis', 'ignorethis.txt')
  88. util.testSinglePlatformParallel('ignore test: RegExp', ignoreTest, /ignorethis/, 'ignorethis.txt')
  89. util.testSinglePlatformParallel('ignore test: Function', ignoreTest,
  90. file => file.match(/ignorethis/), 'ignorethis.txt')
  91. util.testSinglePlatformParallel('ignore test: string with slash', ignoreTest, 'ignore/this',
  92. path.join('ignore', 'this.txt'))
  93. util.testSinglePlatformParallel('ignore test: only match subfolder of app', ignoreTest,
  94. 'electron-packager', path.join('electron-packager', 'readme.txt'))
  95. util.testSinglePlatform('ignore out dir test', ignoreOutDirTest, 'ignoredOutDir')
  96. util.testSinglePlatform('ignore out dir test: unnormalized path', ignoreOutDirTest,
  97. './ignoredOutDir')
  98. util.testSinglePlatform('ignore out dir test: implicit path', ignoreImplicitOutDirTest)