Skip to content

Commit 8e5ce67

Browse files
NathanaelAFatme
authored andcommitted
Fixed Pacote test, broken path.resolve stub breaking things
1 parent bcc0f85 commit 8e5ce67

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

lib/services/pacote-service.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ export class PacoteService implements IPacoteService {
2727
this.$logger.trace(`Calling pacoteService.manifest for packageName: '${packageName}' and options: ${options}`);
2828
const manifestOptions: IPacoteBaseOptions = await this.getPacoteBaseOptions();
2929

30-
// Add NPM Configuration to our Manifest options
31-
_.extend(manifestOptions, this.npmConfig);
3230

3331
if (options) {
3432
_.extend(manifestOptions, options);
@@ -79,7 +77,9 @@ export class PacoteService implements IPacoteService {
7977
private async getPacoteBaseOptions(): Promise<IPacoteBaseOptions> {
8078
// In case `tns create myapp --template https://github.com/NativeScript/template-hello-world.git` command is executed, pacote module throws an error if cache option is not provided.
8179
const cachePath = await this.$packageManager.getCachePath();
82-
const pacoteOptions = { cache: cachePath };
80+
81+
// Add NPM Configuration to our Manifest options
82+
const pacoteOptions = _.extend( this.npmConfig, {cache: cachePath });
8383
const proxySettings = await this.$proxyService.getCache();
8484
if (proxySettings) {
8585
_.extend(pacoteOptions, proxySettings);

test/services/pacote-service.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ const pacote = require("pacote");
1010
const tar = require("tar");
1111
const path = require("path");
1212

13-
const npmCachePath = "npmCachePath";
13+
const defaultPacoteOpts: IPacoteBaseOptions = createPacoteOptions({});
14+
const npmCachePath = defaultPacoteOpts['cache'];
1415
const packageName = "testPackage";
1516
const fullPath = `/Users/username/${packageName}`;
1617
const destinationDir = "destinationDir";
17-
const defaultPacoteOpts: IPacoteBaseOptions = createPacoteOptions({ cache: npmCachePath });
1818
const errorMessage = "error message";
1919
const proxySettings: IProxySettings = {
2020
hostname: "hostname",
@@ -38,11 +38,17 @@ interface ITestCase extends ITestSetup {
3838
expectedArgs: any[];
3939
}
4040

41-
function createPacoteOptions(options: Object): Object {
41+
function createPacoteOptions(source: Object): Object {
42+
let options: { [index: string]: any } = {};
4243
npmconfig.read().forEach((value: any, key: string) => {
4344
// replace env ${VARS} in strings with the process.env value
4445
options[key] = typeof value !== 'string' ? value : value.replace(/\${([^}]+)}/, (_, envVar) => process.env[envVar] );
4546
});
47+
48+
// Copy any original source keys over our defaults
49+
for (let key in source) {
50+
options[key] = source[key];
51+
}
4652
return options;
4753
}
4854

@@ -113,8 +119,15 @@ describe("pacoteService", () => {
113119
const setupTest = (opts?: ITestSetup): IPacoteService => {
114120
opts = opts || {};
115121
const testInjector = createTestInjector(opts);
122+
116123
if (opts.isLocalPackage) {
117-
sandboxInstance.stub(path, "resolve").withArgs(packageName).returns(fullPath);
124+
const oldPath = path.resolve;
125+
sandboxInstance.stub(path, "resolve").callsFake((value:string) => {
126+
if (value === packageName) {
127+
return fullPath;
128+
}
129+
return oldPath(value);
130+
});
118131
}
119132

120133
return testInjector.resolve<IPacoteService>("pacoteService");
@@ -126,7 +139,7 @@ describe("pacoteService", () => {
126139
const testData: ITestCase[] = [
127140
{
128141
name: "with 'cache' only when no opts are passed",
129-
expectedArgs: [packageName, defaultPacoteOpts]
142+
expectedArgs: [packageName, _.extend({}, defaultPacoteOpts)]
130143
},
131144
{
132145
name: "with 'cache' and passed options",
@@ -147,7 +160,7 @@ describe("pacoteService", () => {
147160
{
148161
name: "with full path to file when it is local one",
149162
isLocalPackage: true,
150-
expectedArgs: [fullPath, defaultPacoteOpts]
163+
expectedArgs: [fullPath, _.extend({}, defaultPacoteOpts)]
151164
},
152165
{
153166
name: "with full path to file, 'cache' and passed options when local path is passed",

0 commit comments

Comments
 (0)