danday74 / envsub

NPM - envsub is envsubst for Node.js
Other
62 stars 10 forks source link

Output file does not get created when running in a jest test. #37

Closed luethiandrin closed 1 year ago

luethiandrin commented 1 year ago

I am trying to validate the creation and substitution of both template and output file in a jest test. It works perfectly outside of a jest test --> plain js script. Am I missing out on something or is this a bug?

code(node js):

constant:

module.exports = Object.freeze({
    DEFAULT_ENVIRONMENT_SUBSTITUTION_OPTIONS: {
        all: true,
        diff: true,
        system: true,
        syntax: 'dollar-both', // env pattern: ${} and $
        envs: [{ name: 'release_version' }, { name: 'repo_suffix' }],
    }
});

function:

const substituteEnvForAllFiles = (filePathsArray, options=DEFAULT_ENVIRONMENT_SUBSTITUTION_OPTIONS) => {
    let outputFiles = [];
    filePathsArray.forEach((value) => {
        if (value === '') return;
        const output = value.replaceAll('.template', '');
        envsub({
            templateFile: value,
            outputFile: value.replaceAll('.template', ''),
            options,
        });
        outputFiles.push(output);
    });
    return outputFiles;
};

test:


const customEnvsubstOptions = {
    all: true,
    diff: true,
    system: true,
    syntax: 'dollar-both',
    envs: [{ name: 'test_env' }, { name: 'test_version' }],
};

const templateFilePaths = [
    "<cwd>/.gitlab/node/test/resource/someFileWithEnv-ci.template.yml",
    "<cwd>/.gitlab/node/test/resource/sub-dir/someFileWithEnv-ci.template.yml"
];

const resolvedTemplateFilePaths= [
    "<cwd>/.gitlab/node/test/resource/someFileWithEnv-ci.yml",
    "<cwd>/.gitlab/node/test/resource/sub-dir/someFileWithEnv-ci.yml"
];

it('should substitute all given files and write it out to a new file', () => {
    const outputFiles = substituteEnvForAllFiles(templateFilePaths, customEnvsubstOptions);
    assert.deepEqual(outputFiles, resolvedTemplateFilePaths);
});

files: The yml files to substitute look like this.

.test-template: &test-template
    image: $test_env/bla/bla/image:$test_version
    tags:
        - test
        - nodejs

Thanks already :)