Skip to content

Commit

Permalink
feat(append): adds append mode
Browse files Browse the repository at this point in the history
  • Loading branch information
VannaDii committed Jan 30, 2022
1 parent 66bf2da commit f7beb67
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 33 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ This GitHub Action helps to manipulate yaml files easily.

## Inputs

| name | required | description | default |
| -------- | -------- | --------------------------------------------------------------------- | ------- |
| **file** | `true` | The path to a yaml file | |
| **path** | `true` | The dotted yaml path to edit. Ex: `object.item.name` or `list.0.name` | |
| **get** | `false` | Whether or not to get the value into the output. | `true` |
| **set** | `false` | The value to set at the path. | |
| name | required | description | default |
| ---------- | -------- | --------------------------------------------------------------------- | ------- |
| **file** | `true` | The path to a yaml file | |
| **path** | `true` | The dotted yaml path to edit. Ex: `object.item.name` or `list.0.name` | |
| **get** | `false` | Whether or not to get the value into the output. | `true` |
| **set** | `false` | The value to set at the path. | |
| **append** | `false` | Whether or not the set value is appended or put at the path. | `false` |

## Outputs

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ inputs:
description: "Whether or not to get the value into the output."
required: false
default: "true"
append:
description: "Whether or not the set value is appended or put at the path."
required: false
default: "false"
outputs:
value_old:
description: "If get is true, holds the value found at path prior to changing it, or an empty string if the path was not found."
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type YamlerOptions = {
path: string;
set: any;
get: boolean;
append: boolean;
};

function getOptions(): YamlerOptions {
Expand All @@ -15,6 +16,7 @@ function getOptions(): YamlerOptions {
path: core.getInput('path', { required: true }),
set: core.getInput('set', { required: false }),
get: core.getBooleanInput('get', { required: false }),
append: core.getBooleanInput('append', { required: false }),
};
}

Expand All @@ -34,7 +36,7 @@ export function handleAction() {

if (opts.get) core.setOutput('value_old', pathValue);
if (!!opts.set) {
node[part] = opts.set;
node[part] = opts.append ? `${node[part]}${opts.set}` : opts.set;
if (opts.get) core.setOutput('value_new', node[part]);
}
fs.writeFileSync(opts.file, YAML.stringify(yaml));
Expand Down
113 changes: 88 additions & 25 deletions tests/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,23 @@ dependencies:
outputs[path] = data;
});

expect(handleAction).not.toThrow();
try {
expect(handleAction).not.toThrow();

expect(getInputSpy).toBeCalledTimes(3);
expect(getBooleanInputSpy).toBeCalledTimes(1);
expect(readFileSyncSpy).toBeCalled();
expect(setOutputSpy).toBeCalledTimes(2);
expect(setFailedSpy).toBeCalledTimes(0);
expect(writeFileSyncSpy).toBeCalled();
expect(outputs['value_old']).toEqual('0.0.1');
expect(outputs['value_new']).toEqual(`1.0.${patch}`);
expect(outputs[inputs['file']]).toBeDefined();
[getInputSpy, getBooleanInputSpy, setOutputSpy, setFailedSpy, readFileSyncSpy, writeFileSyncSpy].forEach((s) => {
s.mockRestore();
});
expect(getInputSpy).toBeCalledTimes(3);
expect(getBooleanInputSpy).toBeCalledTimes(2);
expect(readFileSyncSpy).toBeCalled();
expect(setOutputSpy).toBeCalledTimes(2);
expect(setFailedSpy).toBeCalledTimes(0);
expect(writeFileSyncSpy).toBeCalled();
expect(outputs['value_old']).toEqual('0.0.1');
expect(outputs['value_new']).toEqual(`1.0.${patch}`);
expect(outputs[inputs['file']]).toBeDefined();
} finally {
[getInputSpy, getBooleanInputSpy, setOutputSpy, setFailedSpy, readFileSyncSpy, writeFileSyncSpy].forEach((s) => {
s.mockRestore();
});
}
});

it('Basic Nested-Path Op Works As Expected', () => {
Expand Down Expand Up @@ -93,19 +96,79 @@ dependencies:
outputs[path] = data;
});

expect(handleAction).not.toThrow();
try {
expect(handleAction).not.toThrow();

expect(getInputSpy).toBeCalledTimes(3);
expect(getBooleanInputSpy).toBeCalledTimes(2);
expect(readFileSyncSpy).toBeCalled();
expect(setOutputSpy).toBeCalledTimes(2);
expect(setFailedSpy).toBeCalledTimes(0);
expect(writeFileSyncSpy).toBeCalled();
expect(outputs['value_old']).toEqual('^1.7.0');
expect(outputs['value_new']).toEqual(`^2.0.${patch}`);
expect(outputs[inputs['file']]).toBeDefined();
} finally {
[getInputSpy, getBooleanInputSpy, setOutputSpy, setFailedSpy, readFileSyncSpy, writeFileSyncSpy].forEach((s) => {
s.mockRestore();
});
}
});

it('Basic Append Op Works As Expected', () => {
const patch = Date.now();
const outputs: Record<string, string> = {};
const inputs: Record<string, string> = {
file: 'fake.yaml',
path: 'version',
set: `-dev.${patch}`,
get: 'true',
append: 'true',
'fake.yaml': `name: endaft_core
description: The core library behind the EnDaft 'shared' library. Chock full of goodness within.
version: 0.0.1
homepage: https://endaft.dev
dependencies:
meta: ^1.7.0
`,
};

expect(getInputSpy).toBeCalledTimes(3);
expect(getBooleanInputSpy).toBeCalledTimes(1);
expect(readFileSyncSpy).toBeCalled();
expect(setOutputSpy).toBeCalledTimes(2);
expect(setFailedSpy).toBeCalledTimes(0);
expect(writeFileSyncSpy).toBeCalled();
expect(outputs['value_old']).toEqual('^1.7.0');
expect(outputs['value_new']).toEqual(`^2.0.${patch}`);
expect(outputs[inputs['file']]).toBeDefined();
[getInputSpy, getBooleanInputSpy, setOutputSpy, setFailedSpy, readFileSyncSpy, writeFileSyncSpy].forEach((s) => {
s.mockRestore();
const getInputSpy = jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
return inputs[name];
});
const getBooleanInputSpy = jest.spyOn(core, 'getBooleanInput').mockImplementation((name: string) => {
return inputs[name]?.toLowerCase() === 'true';
});
const setOutputSpy = jest.spyOn(core, 'setOutput').mockImplementation((name: string, value: any) => {
outputs[name] = value;
});
const setFailedSpy = jest.spyOn(core, 'setFailed').mockImplementation((message: string | Error) => {
throw message instanceof Error ? message : new Error(message);
});
const readFileSyncSpy = jest.spyOn(fs, 'readFileSync').mockImplementation((path: string) => {
return inputs[path];
});
const writeFileSyncSpy = jest.spyOn(fs, 'writeFileSync').mockImplementation((path: string, data: string) => {
outputs[path] = data;
});

try {
expect(handleAction).not.toThrow();

expect(getInputSpy).toBeCalledTimes(3);
expect(getBooleanInputSpy).toBeCalledTimes(2);
expect(readFileSyncSpy).toBeCalled();
expect(setOutputSpy).toBeCalledTimes(2);
expect(setFailedSpy).toBeCalledTimes(0);
expect(writeFileSyncSpy).toBeCalled();
expect(outputs['value_old']).toEqual('0.0.1');
expect(outputs['value_new']).toEqual(`0.0.1-dev.${patch}`);
expect(outputs[inputs['file']]).toBeDefined();
} finally {
[getInputSpy, getBooleanInputSpy, setOutputSpy, setFailedSpy, readFileSyncSpy, writeFileSyncSpy].forEach((s) => {
s.mockRestore();
});
}
});
});

0 comments on commit f7beb67

Please sign in to comment.