Skip to content

Commit

Permalink
Fix win32 paths in sources (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
qetza authored Mar 16, 2024
1 parent 60659a6 commit f891633
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## v1.1.1
- Fix variable case-sensitivity ([#7](https://github.com/qetza/replacetokens-action/issues/7)).
- Fix paths in sources incompatible with `fast-glob` syntax on win32.

## v1.1.0
- Add support for JSON comments in _variables_ and in JSON variable files and environment variables
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Please refer to the [release page](https://github.com/qetza/replacetokens-action
# A multiline list of files to replace tokens in.
# Each line supports:
# - multiple glob patterns separated by a semi-colon ';' using fast-glob syntax
# (you must always use forward slash '/' as a directory separator)
# (you must always use forward slash '/' as a directory separator, on win32 will
# automatically replace backslash with forward slash)
# - outputing the result in another file adding the output path after an arrow '=>'
# (if the output path is a relative path, it will be relative to the input file)
# - wildcard replacement in the output file name using an asterix '*' in the input
Expand Down
13 changes: 12 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63403,6 +63403,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.run = void 0;
const core = __importStar(__nccwpck_require__(42186));
const rt = __importStar(__nccwpck_require__(49633));
const os = __importStar(__nccwpck_require__(22037));
const strip_json_comments_1 = __importDefault(__nccwpck_require__(30077));
const telemetry_1 = __nccwpck_require__(12417);
const api_1 = __nccwpck_require__(65163);
Expand Down Expand Up @@ -63470,7 +63471,7 @@ async function run() {
suffix: core.getInput('transforms-suffix') || rt.Defaults.TransformSuffix
}
};
const sources = core.getMultilineInput('sources', { required: true, trimWhitespace: true });
const sources = getSources();
const ifNoFilesFound = getChoiceInput('if-no-files-found', ['ignore', 'warn', 'error']) || 'ignore';
const logLevelStr = getChoiceInput('log-level', ['debug', 'info', 'warn', 'error']) || 'info';
// override console logs
Expand Down Expand Up @@ -63579,6 +63580,16 @@ function getChoiceInput(name, choices, options) {
return input;
throw new TypeError(`Unsupported value for input: ${name}\nSupport input list: '${choices.join(' | ')}'`);
}
function getSources() {
const sources = core.getMultilineInput('sources', { required: true, trimWhitespace: true });
// make sources compatible with fast-glob on win32
if (os.platform() === 'win32') {
for (const i in sources) {
sources[i] = sources[i].replace(/\\/g, '/');
}
}
return sources;
}
var variableFilesCount = 0;
var variablesEnvCount = 0;
var inlineVariablesCount = 0;
Expand Down
16 changes: 15 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as core from '@actions/core';
import * as rt from '@qetza/replacetokens';
import * as os from 'os';
import stripJsonComments from './strip-json-comments';
import { TelemetryClient } from './telemetry';
import { SpanStatusCode } from '@opentelemetry/api';
Expand Down Expand Up @@ -84,7 +85,7 @@ export async function run(): Promise<void> {
}
};

const sources = core.getMultilineInput('sources', { required: true, trimWhitespace: true });
const sources = getSources();
const ifNoFilesFound = getChoiceInput('if-no-files-found', ['ignore', 'warn', 'error']) || 'ignore';
const logLevelStr = getChoiceInput('log-level', ['debug', 'info', 'warn', 'error']) || 'info';

Expand Down Expand Up @@ -199,6 +200,19 @@ function getChoiceInput(name: string, choices: string[], options?: core.InputOpt
throw new TypeError(`Unsupported value for input: ${name}\nSupport input list: '${choices.join(' | ')}'`);
}

function getSources(): string[] {
const sources = core.getMultilineInput('sources', { required: true, trimWhitespace: true });

// make sources compatible with fast-glob on win32
if (os.platform() === 'win32') {
for (const i in sources) {
sources[i] = sources[i].replace(/\\/g, '/');
}
}

return sources;
}

var variableFilesCount = 0;
var variablesEnvCount = 0;
var inlineVariablesCount = 0;
Expand Down
26 changes: 26 additions & 0 deletions tests/run.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as core from '@actions/core';
import * as path from 'path';
import * as rt from '@qetza/replacetokens';
import * as os from 'os';
import axios from 'axios';
import { run } from '../src/main';

Expand Down Expand Up @@ -271,6 +272,31 @@ describe('run', () => {
);
});

it('sources: normalize', async () => {
// arrange
const source = path.join(__dirname, 'data', 'vars.json').replace(/\//g, '\\');
getMultilineInputSpy.mockImplementation(name => {
switch (name) {
case 'sources':
return [source];
default:
return [];
}
});

// act
await run();

// assert
expect(setFailedSpy).not.toHaveBeenCalled();

if (os.platform() === 'win32') {
expect(replaceTokenSpy).toHaveBeenCalledWith([source.replace(/\\/g, '/')], expect.anything(), expect.anything());
} else {
expect(replaceTokenSpy).toHaveBeenCalledWith([source], expect.anything(), expect.anything());
}
});

it('variables: object', async () => {
// arrange
const vars = { var1: 'value1', var2: 'value2', SECRET1: 'secret1' };
Expand Down

0 comments on commit f891633

Please sign in to comment.