Skip to content

Commit

Permalink
Ensure relative out dirs are correctly ignored when copying (#919)
Browse files Browse the repository at this point in the history
This happens when both the dir and the out dir are relative to the
current working directory.

Fixes #918.
  • Loading branch information
malept committed Dec 10, 2018
1 parent 5e8526a commit 8d752c2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
5 changes: 3 additions & 2 deletions ignore.js
Expand Up @@ -69,11 +69,12 @@ function userIgnoreFilter (opts) {
const pruner = opts.prune ? new prune.Pruner(opts.dir) : null

return function filter (file) {
if (outIgnores.includes(file)) {
const fullPath = path.resolve(file)
if (outIgnores.includes(fullPath)) {
return false
}

let name = path.resolve(file).split(path.resolve(opts.dir))[1]
let name = fullPath.split(path.resolve(opts.dir))[1]

if (path.sep === '\\') {
name = common.normalizePath(name)
Expand Down
31 changes: 25 additions & 6 deletions test/ignore.js
Expand Up @@ -4,7 +4,6 @@ const common = require('../common')
const fs = require('fs-extra')
const ignore = require('../ignore')
const path = require('path')
const packager = require('..')
const test = require('ava')
const util = require('./_util')

Expand All @@ -24,15 +23,16 @@ function ignoreTest (t, opts, ignorePattern, ignoredFile) {
.then(() => util.assertPathNotExists(t, path.join(targetDir, ignoredFile), `Ignored file '${ignoredFile}' should not exist in copied directory`))
}

function assertOutDirIgnored (t, opts, pathPrefix, existingDirectoryPath, pathToIgnore, ignoredBasenameToCheck) {
function assertOutDirIgnored (t, opts, existingDirectoryPath, pathToIgnore, ignoredBasenameToCheck) {
return fs.copy(util.fixtureSubdir('basic'), t.context.workDir, {
dereference: true,
stopOnErr: true,
filter: file => path.basename(file) !== 'node_modules'
}).then(() => fs.ensureDir(existingDirectoryPath))
// create file to ensure that directory will be not ignored because it's empty
.then(() => fs.writeFile(pathToIgnore, '')).then(() => packager(opts))
.then(() => util.assertPathNotExists(t, path.join(pathPrefix, common.generateFinalBasename(opts), util.generateResourcesPath(opts), 'app', ignoredBasenameToCheck), 'Out dir must not exist in output app directory'))
.then(() => fs.writeFile(pathToIgnore, ''))
.then(() => util.packageAndEnsureResourcesPath(t, opts))
.then(resourcesPath => util.assertPathNotExists(t, path.join(resourcesPath, 'app', ignoredBasenameToCheck), 'Out dir must not exist in output app directory'))
}

function ignoreOutDirTest (t, opts, distPath) {
Expand All @@ -43,7 +43,7 @@ function ignoreOutDirTest (t, opts, distPath) {
// we don't use path.join here to avoid normalizing
opts.out = opts.dir + path.sep + distPath

return assertOutDirIgnored(t, opts, opts.out, opts.out, path.join(opts.out, 'ignoreMe'), path.basename(opts.out))
return assertOutDirIgnored(t, opts, opts.out, path.join(opts.out, 'ignoreMe'), path.basename(opts.out))
}

function ignoreImplicitOutDirTest (t, opts) {
Expand All @@ -54,7 +54,7 @@ function ignoreImplicitOutDirTest (t, opts) {
const testFilename = 'ignoreMe'
const previousPackedResultDir = path.join(opts.dir, `${common.sanitizeAppName(opts.name)}-linux-ia32`)

return assertOutDirIgnored(t, opts, opts.dir, previousPackedResultDir, path.join(previousPackedResultDir, testFilename), testFilename)
return assertOutDirIgnored(t, opts, previousPackedResultDir, path.join(previousPackedResultDir, testFilename), testFilename)
}

test('generateIgnores ignores the generated temporary directory only on Linux', t => {
Expand Down Expand Up @@ -92,3 +92,22 @@ util.testSinglePlatform('ignore out dir test', ignoreOutDirTest, 'ignoredOutDir'
util.testSinglePlatform('ignore out dir test: unnormalized path', ignoreOutDirTest,
'./ignoredOutDir')
util.testSinglePlatform('ignore out dir test: implicit path', ignoreImplicitOutDirTest)
util.testSinglePlatform('ignore out dir test: relative out dir already exists', (t, opts) => {
const oldCWD = process.cwd()
const appDir = path.join(t.context.workDir, 'app')

opts.name = 'ignoredOutDirTest'
opts.dir = '.'
opts.out = 'dir_to_unpack' // already existing out dir
opts.overwrite = true

return fs.copy(util.fixtureSubdir('basic'), appDir)
.then(() => {
process.chdir(appDir)
return util.packageAndEnsureResourcesPath(t, opts)
}).then(resourcesPath => {
process.chdir(oldCWD)
const packagedOutDirPath = path.join(resourcesPath, 'app', opts.out)
return util.assertPathNotExists(t, packagedOutDirPath, `The out dir ${packagedOutDirPath} should not exist in the packaged app`)
})
})

0 comments on commit 8d752c2

Please sign in to comment.