From c058dd6455ffe3c23f9c82197d59c23c01593861 Mon Sep 17 00:00:00 2001 From: Milap Naik Date: Thu, 22 Feb 2024 14:33:57 -0500 Subject: [PATCH] Add defaultsFromJson tests --- src/util.spec.ts | 52 +++++++++++++++++++++++++++++++++++++++++++++++- src/util.ts | 5 +++-- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/util.spec.ts b/src/util.spec.ts index f58c18c..eeb4726 100644 --- a/src/util.spec.ts +++ b/src/util.spec.ts @@ -1,7 +1,7 @@ import { util, standardizePath as s } from './util'; import { expect } from 'chai'; import * as fsExtra from 'fs-extra'; -import { tempDir } from './testUtils.spec'; +import { tempDir, rootDir } from './testUtils.spec'; import * as path from 'path'; import * as dns from 'dns'; import { createSandbox } from 'sinon'; @@ -14,6 +14,7 @@ describe('util', () => { }); afterEach(() => { + fsExtra.emptyDirSync(tempDir); sinon.restore(); }); @@ -334,4 +335,53 @@ describe('util', () => { expect(result).to.eql(expectedOutput); }); }); + + describe('getOptionsFromJson', () => { + beforeEach(() => { + fsExtra.ensureDirSync(rootDir); + process.chdir(rootDir); + }); + it('should fill in missing options from rokudeploy.json', () => { + fsExtra.writeJsonSync(s`${rootDir}/rokudeploy.json`, { password: 'password' }); + let options = util.getOptionsFromJson({ + rootDir: `${rootDir}`, + host: '1.2.3.4' + }); + let expectedOutput = { + rootDir: `${rootDir}`, + host: '1.2.3.4', + password: 'password' + }; + expect(options).to.eql(expectedOutput); + }); + + it('should fill in missing default options from bsconfig.json', () => { + fsExtra.writeJsonSync(s`${rootDir}/bsconfig.json`, { password: 'password' }); + let options = util.getOptionsFromJson({ + rootDir: `${rootDir}`, + host: '1.2.3.4' + }); + let expectedOutput = { + rootDir: `${rootDir}`, + host: '1.2.3.4', + password: 'password' + }; + expect(options).to.eql(expectedOutput); + + }); + + it('should not replace default options', () => { + fsExtra.writeJsonSync(s`${rootDir}/rokudeploy.json`, { host: '4.3.2.1' }); + let options = util.getOptionsFromJson({ + rootDir: `${rootDir}`, + host: '1.2.3.4' + }); + let expectedOutput = { + rootDir: `${rootDir}`, + host: '1.2.3.4', + }; + expect(options).to.eql(expectedOutput); + + }); + }); }); diff --git a/src/util.ts b/src/util.ts index e807ff4..9fd883b 100644 --- a/src/util.ts +++ b/src/util.ts @@ -492,9 +492,10 @@ export class Util { for (const fileName of fileNames) { if (fsExtra.existsSync(fileName)) { - let rokudeployArgs = JSON.parse(fs.readFileSync('rokudeploy.json', 'utf-8')); + const sourcePath = path.join(defaultArgs.rootDir, fileName); + const rokuDeployArgs = JSON.parse(fsExtra.readFileSync(sourcePath, 'utf8')); // args = Object.assign(rokudeployArgs ?? {}, args); - args = Object.assign(rokudeployArgs, args); + args = Object.assign(rokuDeployArgs, args); break; } }