Skip to content

Commit

Permalink
feat(core-manager): implement snapshots.delete action (#3724)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastijankuzner authored May 25, 2020
1 parent 82382fc commit f87e146
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
47 changes: 47 additions & 0 deletions __tests__/unit/core-manager/actions/snapshots-delete.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import "jest-extended";

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

let sandbox: Sandbox;
let action: Action;

let mockFilesystem;

beforeEach(() => {
mockFilesystem = {
exists: jest.fn().mockResolvedValue(true),
delete: jest.fn().mockResolvedValue(true),
};

sandbox = new Sandbox();

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

action = sandbox.app.resolve(Action);
});

describe("Snapshots:Delete", () => {
it("should have name", () => {
expect(action.name).toEqual("snapshots.delete");
});

it("should delete snapshot", async () => {
const result = await action.execute({ name: "1-10" });

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

it("should throw error if snapshot is not found", async () => {
mockFilesystem.exists = jest.fn().mockResolvedValue(false);

await expect(action.execute({ name: "1-10" })).rejects.toThrow("Snapshot not found");
});

it("should throw error if snapshot is not deleted", async () => {
mockFilesystem.delete = jest.fn().mockResolvedValue(false);

await expect(action.execute({ name: "1-10" })).rejects.toThrow("Cannot delete snapshot");
});
});
41 changes: 41 additions & 0 deletions packages/core-manager/src/actions/snaphsots-delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Container, Contracts } from "@arkecosystem/core-kernel";
import { join } from "path";

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

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

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

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

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

return {};
}

public async deleteSnapshot(name: string): Promise<void> {
const snapshotsDir = `${process.env.CORE_PATH_DATA}/snapshots/`;
const snapshotPath = join(snapshotsDir, name);

if (!(await this.filesystem.exists(snapshotPath))) {
throw new Error("Snapshot not found");
}

if (!(await this.filesystem.delete(snapshotPath))) {
throw new Error("Cannot delete snapshot");
}
}
}
2 changes: 1 addition & 1 deletion packages/core/src/commands/core-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class Command extends Commands.Command {
const flags: Contracts.AnyObject = { ...this.getFlags() };
flags.processType = "core";

this.actions.abortRunningProcess(`${flags.token}-core`);
// this.actions.abortRunningProcess(`${flags.token}-core`);

await Utils.buildApplication({
flags,
Expand Down

0 comments on commit f87e146

Please sign in to comment.