@@ -4,42 +4,121 @@ const { describe, it } = require('../helpers/mocha');
44const { expect } = require ( '../helpers/chai' ) ;
55const getBlueprintNameOverride = require ( '../../src/get-default-blueprint-name-override' ) ;
66const sinon = require ( 'sinon' ) ;
7+ const path = require ( 'path' ) ;
8+ const fs = require ( 'fs-extra' ) ;
79const npm = require ( 'boilerplate-update/src/npm' ) ;
810
9- const packageName = 'a-package-name' ;
11+ const cwd = 'a/made/up/path' ;
12+ const packageNameOrPath = '../a-package-name' ;
13+ const defaultBlueprint = 'not-the-same-name' ;
14+ const localPackageJsonPath = path . join ( path . resolve ( cwd , packageNameOrPath ) , 'package.json' ) ;
1015
1116describe ( getBlueprintNameOverride , function ( ) {
17+ let pathExistsStub ;
18+ let readFileStub ;
19+ let jsonStub ;
20+
21+ beforeEach ( function ( ) {
22+ pathExistsStub = sinon . stub ( fs , 'pathExists' ) ;
23+ readFileStub = sinon . stub ( fs , 'readFile' ) ;
24+ jsonStub = sinon . stub ( npm , 'json' ) ;
25+ } ) ;
26+
1227 afterEach ( function ( ) {
1328 sinon . restore ( ) ;
1429 } ) ;
1530
1631 it ( 'returns default blueprint override name if it exists' , async function ( ) {
17- sinon . stub ( npm , 'json' ) . resolves ( {
18- name : packageName ,
32+ pathExistsStub . withArgs ( localPackageJsonPath ) . resolves ( true ) ;
33+
34+ readFileStub . withArgs ( localPackageJsonPath ) . resolves ( JSON . stringify ( {
35+ name : packageNameOrPath ,
1936 'ember-addon' : {
20- defaultBlueprint : 'not-the-same-name'
37+ defaultBlueprint
38+ }
39+ } ) ) ;
40+
41+ let defaultBlueprintOverride = await getBlueprintNameOverride ( packageNameOrPath , cwd ) ;
42+
43+ expect ( defaultBlueprintOverride ) . to . equal ( defaultBlueprint ) ;
44+ } ) ;
45+
46+ it ( 'doesn\'t use npm if found locally' , async function ( ) {
47+ pathExistsStub . withArgs ( localPackageJsonPath ) . resolves ( true ) ;
48+
49+ readFileStub . withArgs ( localPackageJsonPath ) . resolves ( JSON . stringify ( {
50+ name : packageNameOrPath ,
51+ 'ember-addon' : {
52+ defaultBlueprint
53+ }
54+ } ) ) ;
55+
56+ await getBlueprintNameOverride ( packageNameOrPath , cwd ) ;
57+
58+ expect ( jsonStub ) . to . not . have . been . called ;
59+ } ) ;
60+
61+ it ( 'uses npm if not found locally' , async function ( ) {
62+ pathExistsStub . withArgs ( localPackageJsonPath ) . resolves ( false ) ;
63+
64+ jsonStub . withArgs ( 'view' , packageNameOrPath ) . resolves ( {
65+ name : packageNameOrPath ,
66+ 'ember-addon' : {
67+ defaultBlueprint
2168 }
2269 } ) ;
2370
24- let defaultBlueprintOverride = await getBlueprintNameOverride ( packageName ) ;
71+ let defaultBlueprintOverride = await getBlueprintNameOverride ( packageNameOrPath , cwd ) ;
2572
26- expect ( defaultBlueprintOverride ) . to . equal ( 'not-the-same-name' ) ;
73+ expect ( defaultBlueprintOverride ) . to . equal ( defaultBlueprint ) ;
2774 } ) ;
2875
29- it ( 'Null if property does not exist in package.json' , async function ( ) {
30- sinon . stub ( npm , 'json' ) . resolves ( {
31- name : packageName
76+ it ( 'doesn\'t read local file if using npm' , async function ( ) {
77+ pathExistsStub . withArgs ( localPackageJsonPath ) . resolves ( false ) ;
78+
79+ jsonStub . withArgs ( 'view' , packageNameOrPath ) . resolves ( {
80+ name : packageNameOrPath ,
81+ 'ember-addon' : {
82+ defaultBlueprint
83+ }
3284 } ) ;
3385
34- let defaultBlueprintOverride = await getBlueprintNameOverride ( packageName ) ;
86+ await getBlueprintNameOverride ( packageNameOrPath , cwd ) ;
87+
88+ expect ( readFileStub ) . to . not . have . been . called ;
89+ } ) ;
90+
91+ it ( 'Null if ember-addon does not exist in package.json' , async function ( ) {
92+ pathExistsStub . withArgs ( localPackageJsonPath ) . resolves ( true ) ;
93+
94+ readFileStub . withArgs ( localPackageJsonPath ) . resolves ( JSON . stringify ( {
95+ name : packageNameOrPath
96+ } ) ) ;
97+
98+ let defaultBlueprintOverride = await getBlueprintNameOverride ( packageNameOrPath , cwd ) ;
99+
100+ expect ( defaultBlueprintOverride ) . to . be . null ;
101+ } ) ;
102+
103+ it ( 'Null if defaultBlueprint does not exist in ember-addon' , async function ( ) {
104+ pathExistsStub . withArgs ( localPackageJsonPath ) . resolves ( true ) ;
105+
106+ readFileStub . withArgs ( localPackageJsonPath ) . resolves ( JSON . stringify ( {
107+ name : packageNameOrPath ,
108+ 'ember-addon' : { }
109+ } ) ) ;
110+
111+ let defaultBlueprintOverride = await getBlueprintNameOverride ( packageNameOrPath , cwd ) ;
35112
36113 expect ( defaultBlueprintOverride ) . to . be . null ;
37114 } ) ;
38115
39116 it ( 'Error in spawn returns null' , async function ( ) {
40- sinon . stub ( npm , 'json' ) . rejects ( ) ;
117+ pathExistsStub . withArgs ( localPackageJsonPath ) . resolves ( false ) ;
118+
119+ jsonStub . withArgs ( 'view' , packageNameOrPath ) . rejects ( ) ;
41120
42- let defaultBlueprintOverride = await getBlueprintNameOverride ( packageName ) ;
121+ let defaultBlueprintOverride = await getBlueprintNameOverride ( packageNameOrPath , cwd ) ;
43122
44123 expect ( defaultBlueprintOverride ) . to . be . null ;
45124 } ) ;
0 commit comments