-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core-manager): implement
configuration.updateEnv
action (#3718)
- Loading branch information
1 parent
dcfff76
commit ca28f66
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
__tests__/unit/core-manager/actions/configuration-update-env.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
51
packages/core-manager/src/actions/configuration-update-env.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |