-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* add tests for pack with files-array * include mandatory files if not in files-array npm pack includes some files even if they are not included in the “files” field in package.json. This commit creates the same behaviour in yarn pack. * explicitly exclude dotfiles in pack with files if files is used in package.json all other files should be excluded. Files with dot as first character have to be excluded explicitly because minimatch will not match them by default.
- Loading branch information
Showing
28 changed files
with
265 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
/* @flow */ | ||
import type {Reporter} from '../../src/reporters/index.js'; | ||
|
||
import * as fs from '../../src/util/fs.js'; | ||
import assert from 'assert'; | ||
import {run as pack} from '../../src/cli/commands/pack.js'; | ||
import * as reporters from '../../src/reporters/index.js'; | ||
import Config from '../../src/config.js'; | ||
|
||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000; | ||
|
||
const path = require('path'); | ||
const os = require('os'); | ||
const stream = require('stream'); | ||
|
||
const zlib = require('zlib'); | ||
const tar = require('tar'); | ||
const fs2 = require('fs'); | ||
|
||
const fixturesLoc = path.join(__dirname, '..', 'fixtures', 'pack'); | ||
|
||
export function runPack( | ||
flags: Object, | ||
name: string, | ||
checkArchive?: ?(config: Config) => ?Promise<void>, | ||
): Promise<void> { | ||
return run(() => { | ||
return pack; | ||
}, flags, path.join(fixturesLoc, name), checkArchive); | ||
} | ||
|
||
export async function run( | ||
factory: () => (config: Config, reporter: Reporter, flags: Object, args: Array<string>) => Promise<void>, | ||
flags: Object, | ||
dir: string, | ||
checkArchive: ?(config: Config) => ?Promise<void>, | ||
): Promise<void> { | ||
let out = ''; | ||
const stdout = new stream.Writable({ | ||
decodeStrings: false, | ||
write(data, encoding, cb) { | ||
out += data; | ||
cb(); | ||
}, | ||
}); | ||
|
||
const reporter = new reporters.ConsoleReporter({stdout, stderr: stdout}); | ||
|
||
const cwd = path.join( | ||
os.tmpdir(), | ||
`yarn-${path.basename(dir)}-${Math.random()}`, | ||
); | ||
await fs.unlink(cwd); | ||
await fs.copy(dir, cwd, reporter); | ||
|
||
for (const {basename, absolute} of await fs.walk(cwd)) { | ||
if (basename.toLowerCase() === '.ds_store') { | ||
await fs.unlink(absolute); | ||
} | ||
} | ||
|
||
try { | ||
const config = new Config(reporter); | ||
await config.init({ | ||
cwd, | ||
globalFolder: path.join(cwd, '.yarn/.global'), | ||
cacheFolder: path.join(cwd, '.yarn'), | ||
linkFolder: path.join(cwd, '.yarn/.link'), | ||
}); | ||
|
||
await pack(config, reporter, flags, []); | ||
|
||
try { | ||
if (checkArchive) { | ||
await checkArchive(config); | ||
} | ||
} finally { | ||
// clean up | ||
await fs.unlink(cwd); | ||
} | ||
} catch (err) { | ||
throw new Error(`${err && err.stack} \nConsole output:\n ${out}`); | ||
} | ||
} | ||
|
||
export async function getFilesFromArchive(source, destination): Promise<Array<string>> { | ||
const unzip = new Promise((resolve, reject) => { | ||
fs2.createReadStream(source) | ||
.pipe(zlib.createUnzip()) | ||
.pipe(tar.Extract({path: destination, strip: 1})) | ||
.on('end', () => { | ||
resolve(); | ||
}) | ||
.on('error', (error) => { | ||
reject(error); | ||
}); | ||
}); | ||
await unzip; | ||
const files = await fs.readdir(destination); | ||
return files; | ||
} | ||
|
||
// skip for now because this test fails on travis | ||
test.concurrent.skip('pack should work with a minimal example', (): Promise<void> => { | ||
return runPack({}, 'minimal', async (config): Promise<void> => { | ||
const {cwd} = config; | ||
const files = await getFilesFromArchive( | ||
path.join(cwd, 'pack-minimal-test-v1.0.0.tgz'), | ||
path.join(cwd, 'pack-minimal-test-v1.0.0'), | ||
); | ||
|
||
assert.ok(files); | ||
}); | ||
}); | ||
|
||
test.concurrent('pack should inlude all files listed in the files array', (): Promise<void> => { | ||
return runPack({}, 'files-include', async (config): Promise<void> => { | ||
const {cwd} = config; | ||
const files = await getFilesFromArchive( | ||
path.join(cwd, 'files-include-v1.0.0.tgz'), | ||
path.join(cwd, 'files-include-v1.0.0'), | ||
); | ||
const expected = ['index.js', 'a.js', 'b.js']; | ||
expected.forEach((filename) => { | ||
assert(files.indexOf(filename) >= 0); | ||
}); | ||
}); | ||
}); | ||
|
||
test.concurrent('pack should include mandatory files not listed in files array if files not empty', | ||
(): Promise<void> => { | ||
return runPack({}, 'files-include-mandatory', async (config): Promise<void> => { | ||
const {cwd} = config; | ||
const files = await getFilesFromArchive( | ||
path.join(cwd, 'files-include-mandatory-v1.0.0.tgz'), | ||
path.join(cwd, 'files-include-mandatory-v1.0.0'), | ||
); | ||
const expected = ['package.json', 'readme.md', 'license', 'changelog']; | ||
expected.forEach((filename) => { | ||
assert(files.indexOf(filename) >= 0); | ||
}); | ||
}); | ||
}); | ||
|
||
test.concurrent('pack should exclude all other files if files array is not empty', | ||
(): Promise<void> => { | ||
return runPack({}, 'files-exclude', async (config): Promise<void> => { | ||
const {cwd} = config; | ||
const files = await getFilesFromArchive( | ||
path.join(cwd, 'files-exclude-v1.0.0.tgz'), | ||
path.join(cwd, 'files-exclude-v1.0.0'), | ||
); | ||
const excluded = ['c.js']; | ||
excluded.forEach((filename) => { | ||
assert(!(files.indexOf(filename) >= 0)); | ||
}); | ||
}); | ||
}); | ||
|
||
test.concurrent('pack should exclude all dotflies if not in files and files not empty', | ||
(): Promise<void> => { | ||
return runPack({}, 'files-exclude-dotfile', async (config): Promise<void> => { | ||
const {cwd} = config; | ||
const files = await getFilesFromArchive( | ||
path.join(cwd, 'files-exclude-dotfile-v1.0.0.tgz'), | ||
path.join(cwd, 'files-exclude-dotfile-v1.0.0'), | ||
); | ||
assert(!(files.indexOf('.dotfile') >= 0)); | ||
}); | ||
}); | ||
|
||
test.concurrent('pack should exclude all files in dot-directories if not in files and files not empty ', | ||
(): Promise<void> => { | ||
return runPack({}, 'files-exclude-dotdir', async (config): Promise<void> => { | ||
const {cwd} = config; | ||
const files = await getFilesFromArchive( | ||
path.join(cwd, 'files-exclude-dotdir-v1.0.0.tgz'), | ||
path.join(cwd, 'files-exclude-dotdir-v1.0.0'), | ||
); | ||
assert(!(files.indexOf('a.js') >= 0)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* @flow */ | ||
console.log('hello world'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* @flow */ | ||
console.log('hello world'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "files-exclude-dotdir", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"files": ["index.js", "a.js", "b.js"] | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* @flow */ | ||
console.log('hello world'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* @flow */ | ||
console.log('hello world'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* @flow */ | ||
console.log('hello world'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* @flow */ | ||
console.log('hello world'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "files-exclude-dotfile", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"files": ["index.js", "a.js", "b.js"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* @flow */ | ||
console.log('hello world'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* @flow */ | ||
console.log('hello world'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* @flow */ | ||
console.log('hello world'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* @flow */ | ||
console.log('hello world'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "files-exclude", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"files": ["index.js", "a.js", "b.js"] | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* @flow */ | ||
console.log('hello world'); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "files-include-mandatory", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"files": ["index.js", "a.js", "b.js"] | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* @flow */ | ||
console.log('hello world'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* @flow */ | ||
console.log('hello world'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* @flow */ | ||
console.log('hello world'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log('hello world'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "files-include", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"files": ["index.js", "a.js", "b.js"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log('hello world'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "pack-minimal-test", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters