|
| 1 | +/* |
| 2 | + Copyright 2025 Google LLC |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import {deepStrictEqual} from 'assert'; |
| 18 | +import setupVars from './setup-vars.js'; |
| 19 | +import {substituteVars, uniqueId} from './setup-vars.js'; |
| 20 | + |
| 21 | +const projectId = 'my-test-project'; |
| 22 | +const core = { |
| 23 | + exportVariable: (_key, _value) => null, |
| 24 | +}; |
| 25 | + |
| 26 | +const autovars = {PROJECT_ID: projectId, RUN_ID: 'run-id'}; |
| 27 | + |
| 28 | +describe('setupVars', () => { |
| 29 | + describe('env', () => { |
| 30 | + it('empty', () => { |
| 31 | + const setup = {}; |
| 32 | + const vars = setupVars({projectId, core, setup}, 'run-id'); |
| 33 | + const expected = autovars; |
| 34 | + deepStrictEqual(vars.env, expected); |
| 35 | + }); |
| 36 | + |
| 37 | + it('zero vars', () => { |
| 38 | + const setup = {env: {}}; |
| 39 | + const vars = setupVars({projectId, core, setup}, 'run-id'); |
| 40 | + const expected = autovars; |
| 41 | + deepStrictEqual(vars.env, expected); |
| 42 | + }); |
| 43 | + |
| 44 | + it('one var', () => { |
| 45 | + const setup = {env: {A: 'x'}}; |
| 46 | + const vars = setupVars({projectId, core, setup}, 'run-id'); |
| 47 | + const expected = {...autovars, A: 'x'}; |
| 48 | + deepStrictEqual(vars.env, expected); |
| 49 | + }); |
| 50 | + |
| 51 | + it('three vars', () => { |
| 52 | + const setup = {env: {A: 'x', B: 'y', C: 'z'}}; |
| 53 | + const vars = setupVars({projectId, core, setup}, 'run-id'); |
| 54 | + const expected = {...autovars, A: 'x', B: 'y', C: 'z'}; |
| 55 | + deepStrictEqual(vars.env, expected); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should override automatic variables', () => { |
| 59 | + const setup = {env: {PROJECT_ID: 'custom-value'}}; |
| 60 | + const vars = setupVars({projectId, core, setup}, 'run-id'); |
| 61 | + const expected = {PROJECT_ID: 'custom-value', RUN_ID: 'run-id'}; |
| 62 | + deepStrictEqual(vars.env, expected); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should interpolate variables', () => { |
| 66 | + const setup = {env: {A: 'x', B: 'y', C: '$A/${B}'}}; |
| 67 | + const vars = setupVars({projectId, core, setup}, 'run-id'); |
| 68 | + const expected = {...autovars, A: 'x', B: 'y', C: 'x/y'}; |
| 69 | + deepStrictEqual(vars.env, expected); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should not interpolate secrets', () => { |
| 73 | + const setup = { |
| 74 | + env: {C: '$x/$y'}, |
| 75 | + secrets: {A: 'x', B: 'y'}, |
| 76 | + }; |
| 77 | + const vars = setupVars({projectId, core, setup}, 'run-id'); |
| 78 | + const expected = {...autovars, C: '$x/$y'}; |
| 79 | + deepStrictEqual(vars.env, expected); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('secrets', () => { |
| 84 | + it('zero secrets', () => { |
| 85 | + const setup = {secrets: {}}; |
| 86 | + const vars = setupVars({projectId, core, setup}, 'run-id'); |
| 87 | + deepStrictEqual(vars.secrets, ''); |
| 88 | + }); |
| 89 | + |
| 90 | + it('one secret', () => { |
| 91 | + const setup = {secrets: {A: 'x'}}; |
| 92 | + const vars = setupVars({projectId, core, setup}, 'run-id'); |
| 93 | + const expected = 'A:x'; |
| 94 | + deepStrictEqual(vars.secrets, expected); |
| 95 | + }); |
| 96 | + |
| 97 | + it('three secrets', () => { |
| 98 | + const setup = {secrets: {A: 'x', B: 'y', C: 'z'}}; |
| 99 | + const vars = setupVars({projectId, core, setup}, 'run-id'); |
| 100 | + const expected = 'A:x\nB:y\nC:z'; |
| 101 | + deepStrictEqual(vars.secrets, expected); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should not interpolate variables', () => { |
| 105 | + const setup = { |
| 106 | + env: {A: 'x', B: 'y'}, |
| 107 | + secrets: {C: '$A/$B'}, |
| 108 | + }; |
| 109 | + const vars = setupVars({projectId, core, setup}, 'run-id'); |
| 110 | + const expected = 'C:$A/$B'; |
| 111 | + deepStrictEqual(vars.secrets, expected); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should not interpolate secrets', () => { |
| 115 | + const setup = {secrets: {A: 'x', B: 'y', C: '$A/$B'}}; |
| 116 | + const vars = setupVars({projectId, core, setup}, 'run-id'); |
| 117 | + const expected = 'A:x\nB:y\nC:$A/$B'; |
| 118 | + deepStrictEqual(vars.secrets, expected); |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +describe('substituteVars', () => { |
| 124 | + it('should interpolate $VAR', () => { |
| 125 | + const got = substituteVars('$A-$B', {A: 'x', B: 'y'}); |
| 126 | + const expected = 'x-y'; |
| 127 | + deepStrictEqual(got, expected); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should interpolate ${VAR}', () => { |
| 131 | + const got = substituteVars('${A}-${B}', {A: 'x', B: 'y'}); |
| 132 | + const expected = 'x-y'; |
| 133 | + deepStrictEqual(got, expected); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should interpolate ${ VAR }', () => { |
| 137 | + const got = substituteVars('${ A }-${ \tB\t }', {A: 'x', B: 'y'}); |
| 138 | + const expected = 'x-y'; |
| 139 | + deepStrictEqual(got, expected); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should not interpolate on non-word boundary', () => { |
| 143 | + const got = substituteVars('$Ab', {A: 'x'}); |
| 144 | + const expected = '$Ab'; |
| 145 | + deepStrictEqual(got, expected); |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
| 149 | +describe('uniqueId', () => { |
| 150 | + it('should match length', () => { |
| 151 | + const n = 6; |
| 152 | + deepStrictEqual(uniqueId(n).length, n); |
| 153 | + }); |
| 154 | +}); |
0 commit comments