Skip to content

Commit

Permalink
start tests
Browse files Browse the repository at this point in the history
  • Loading branch information
austencollins committed Nov 7, 2016
1 parent fe26deb commit 59cebc0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 26 deletions.
40 changes: 14 additions & 26 deletions lib/classes/Variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,21 +163,22 @@ class Variables {
return undefined;
}

// Validate file extension is supported
if (['yml','json','js'].indexOf(fileExtension) == -1) {
const errorMessage = [
'Invalid variable reference to another file.',
`"${referencedFileRelativePath}" has an unsupported extension`,
' Please use .yml, .json, or .js',
].join('');
throw new this.serverless.classes
.Error(errorMessage);
}

let valueToPopulate;

// Process YML or JSON
if (fileExtension == 'yml' || fileExtension == 'json') {
// Process JS files
if (fileExtension == 'js') {
let jsFile = require(referencedFileFullPath);
let jsModule = variableString.split(':')[1];
jsModule = jsModule.split('.')[0];
valueToPopulate = jsFile[jsModule]();
let deepProperties = variableString.replace(matchedFileRefString, '')
deepProperties = deepProperties.slice(1).split('.');
deepProperties.splice(0, 1)
valueToPopulate = this.getDeepValue(deepProperties, valueToPopulate)
}

// Process everything except JS
if (fileExtension !== 'js') {

valueToPopulate = this.serverless.utils.readFileSync(referencedFileFullPath);
if (matchedFileRefString !== variableString) {
Expand All @@ -197,19 +198,6 @@ class Variables {
}
}

// Process JS
if (fileExtension == 'js') {
let jsFile = require(referencedFileFullPath);
let jsModule = variableString.split(':')[1];
jsModule = jsModule.split('.')[0];
valueToPopulate = jsFile[jsModule]();
let deepProperties = variableString.replace(matchedFileRefString, '')
deepProperties = deepProperties.slice(1).split('.');
deepProperties.splice(0, 1)
valueToPopulate = this.getDeepValue(deepProperties, valueToPopulate)
}

console.log(valueToPopulate)
return valueToPopulate;
}

Expand Down
31 changes: 31 additions & 0 deletions lib/classes/Variables.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,37 @@ describe('Variables', () => {
expect(valueToPopulate).to.equal(2);
});

it('should populate from a javascript file', () => {
const serverless = new Serverless();
const SUtils = new Utils();
const tmpDirPath = testUtils.getTmpDirPath();
const jsData = 'module.exports.hello=function(){return "hello world";};'

SUtils.writeFileSync(path.join(tmpDirPath, 'hello.js'), jsData);

serverless.config.update({ servicePath: tmpDirPath });

const valueToPopulate = serverless.variables
.getValueFromFile('file(./hello.yml):hello');
expect(valueToPopulate).to.equal('hello world');
});

it('should populate deep object from a javascript file', () => {
const serverless = new Serverless();
const SUtils = new Utils();
const tmpDirPath = testUtils.getTmpDirPath();
const jsData = `module.exports.hello=function(){
return {one:{two:{three: 'hello world'}}}
};`

SUtils.writeFileSync(path.join(tmpDirPath, 'hello.js'), jsData);

serverless.config.update({ servicePath: tmpDirPath });
const valueToPopulate = serverless.variables
.getValueFromFile('file(./hello.js):hello.one.two.three');
expect(valueToPopulate).to.equal('hello world');
});

it('should throw error if not using ":" syntax', () => {
const serverless = new Serverless();
const SUtils = new Utils();
Expand Down

0 comments on commit 59cebc0

Please sign in to comment.