Skip to content

Commit

Permalink
feat(core-manager): implement configuration.updateEnv action (#3718)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastijankuzner committed May 20, 2020
1 parent dcfff76 commit ca28f66
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import "jest-extended";

import { Container } from "@packages/core-kernel";
import { Action } from "@packages/core-manager/src/actions/configuration-update-env";
import { Sandbox } from "@packages/core-test-framework";

let sandbox: Sandbox;
let action: Action;

const mockFilesystem = {
put: jest.fn(),
};

beforeEach(() => {
sandbox = new Sandbox();

sandbox.app.bind(Container.Identifiers.FilesystemService).toConstantValue(mockFilesystem);

action = sandbox.app.resolve(Action);

sandbox.app.environmentFile = jest.fn().mockReturnValue("/path/to/file");
});

describe("Configuration:UpdateEnv", () => {
it("should have name", () => {
expect(action.name).toEqual("configuration.updateEnv");
});

it("should validate and save configuration", async () => {
const content = "ABC=1\n\nABD=2";

const result = await action.execute({ content: content });

expect(result).toEqual({});
});

it("should throw validation error - variable is lowercase", async () => {
const content = "abc=1";
await expect(action.execute({ content: content })).rejects.toThrow();
});

it("should throw validation error - variable contains invalid chars", async () => {
const content = "ABC.D=1";
await expect(action.execute({ content: content })).rejects.toThrow();
});

it("should throw validation error - invalid characters (space) after expression", async () => {
const content = "ABC=1 ";
await expect(action.execute({ content: content })).rejects.toThrow();
});

it("should throw validation error - value is not set", async () => {
const content = "ABC=";
await expect(action.execute({ content: content })).rejects.toThrow();
});
});
51 changes: 51 additions & 0 deletions packages/core-manager/src/actions/configuration-update-env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Application, Container, Contracts } from "@arkecosystem/core-kernel";

import { Actions } from "../contracts";

@Container.injectable()
export class Action implements Actions.Action {
public name = "configuration.updateEnv";

public schema = {
type: "object",
properties: {
content: {
type: "string",
},
},
required: ["content"],
};

@Container.inject(Container.Identifiers.Application)
private readonly app!: Application;

@Container.inject(Container.Identifiers.FilesystemService)
private readonly filesystem!: Contracts.Kernel.Filesystem;

public async execute(params: { content: string }): Promise<any> {
await this.updateEnv(params.content);

return {};
}

private validateEnv(content: string): void {
let count = 0;
for (const line of content.toString().split("\n")) {
count++;
if (line === "") {
continue;
}
const matches: RegExpExecArray | null = new RegExp(/^[A-Z][A-Z0-9_]*=\S\S*$/).exec(line);

if (!matches) {
throw new Error(`Invalid line [${count}]: ${line}`);
}
}
}

private async updateEnv(content: string): Promise<void> {
this.validateEnv(content);

await this.filesystem.put(this.app.environmentFile(), content);
}
}

0 comments on commit ca28f66

Please sign in to comment.