Skip to content

Commit

Permalink
remove cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
ComradeVanti committed Aug 23, 2024
1 parent 8ddfe58 commit e641666
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 71 deletions.
32 changes: 31 additions & 1 deletion src/services/remove-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function RemovePackagesFromManifest(

manifest = removeDependency(manifest, packageName);

if (manifest.scopedRegistries !== undefined)
if (manifest.scopedRegistries !== undefined) {
manifest = {
...manifest,
scopedRegistries: manifest.scopedRegistries
Expand All @@ -75,6 +75,36 @@ export function RemovePackagesFromManifest(
})),
};

// Remove scoped registries without scopes
manifest = {
...manifest,
scopedRegistries: manifest.scopedRegistries!.filter(
(it) => it.scopes.length > 0
),
};

// Remove scoped registries property if empty
if (manifest.scopedRegistries!.length === 0) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { scopedRegistries, ...withoutScopedRegistries } = manifest;
manifest = withoutScopedRegistries;
}
}

if (manifest.testables !== undefined) {
manifest = {
...manifest,
testables: manifest.testables.filter((it) => it !== packageName),
};

// Remove testables property if empty
if (manifest.testables!.length === 0) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { testables, ...withoutTestables } = manifest;
manifest = withoutTestables;
}
}

return Ok([manifest, { name: packageName, version: versionInManifest }]);
};

Expand Down
91 changes: 91 additions & 0 deletions test/e2e/remove.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { ResultCodes } from "../../src/cli/result-codes";
import { emptyProjectManifest } from "../../src/domain/project-manifest";
import { loadProjectManifestUsing } from "../../src/io/project-manifest-io";
import { buildProjectManifest } from "../unit/domain/data-project-manifest";
import { getProjectManifest } from "./check/project-manifest";
import { runOpenupm } from "./run";
import { prepareHomeDirectory } from "./setup/directories";
import { prepareUnityProject } from "./setup/project";

describe("remove packages", () => {
it("should not accept package reference with version", async () => {
const homeDirectory = await prepareHomeDirectory();
const projectDirectory = await prepareUnityProject(homeDirectory);

const result = await runOpenupm(projectDirectory, [
"remove",
"dev.comradevanti.opt-unity@2.0.0",
]);

expect(result.code).toEqual(ResultCodes.Error);
});

it("should remove package from manifest", async () => {
const homeDirectory = await prepareHomeDirectory();
const projectDirectory = await prepareUnityProject(homeDirectory, {
manifest: buildProjectManifest((manifest) =>
manifest.addDependency(
"dev.comradevanti.opt-unity",
"2.0.0",
true,
true
)
),
});

const result = await runOpenupm(projectDirectory, [
"remove",
"dev.comradevanti.opt-unity",
]);

const actualManifest = await getProjectManifest(projectDirectory);
expect(result.code).toEqual(ResultCodes.Ok);
expect(result.stdErr).toEqual(
expect.arrayContaining([
expect.stringContaining('Removed "dev.comradevanti.opt-unity@2.0.0"'),
expect.stringContaining("please open Unity"),
])
);
expect(actualManifest).toEqual(emptyProjectManifest);
});

it("should fail if package is not in manifest", async () => {
const homeDirectory = await prepareHomeDirectory();
const projectDirectory = await prepareUnityProject(homeDirectory);

const result = await runOpenupm(projectDirectory, [
"remove",
"dev.comradevanti.opt-unity",
]);

expect(result.code).toEqual(ResultCodes.Error);
expect(result.stdErr).toEqual(
expect.arrayContaining([
expect.stringContaining(
'Package "dev.comradevanti.opt-unity" could not be found'
),
expect.stringContaining("Did you make a typo"),
])
);
});

it("should be atomic", async () => {
const homeDirectory = await prepareHomeDirectory();
const initialManifest = buildProjectManifest((manifest) =>
manifest.addDependency("dev.comradevanti.opt-unity", "2.0.0", true, true)
);
const projectDirectory = await prepareUnityProject(homeDirectory, {
manifest: initialManifest,
});

const result = await runOpenupm(projectDirectory, [
"remove",
"dev.comradevanti.opt-unity",
"other.unknown.package",
]);

const actualManifest = await getProjectManifest(projectDirectory);
expect(result.code).toEqual(ResultCodes.Error);
expect(actualManifest).toEqual(initialManifest);
});
});
13 changes: 10 additions & 3 deletions test/e2e/setup/project.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import fse from "fs-extra";
import path from "path";
import yaml from "yaml";
import { emptyProjectManifest } from "../../../src/domain/project-manifest";
import {
emptyProjectManifest,
UnityProjectManifest,
} from "../../../src/domain/project-manifest";
import { WriteProjectManifestFile } from "../../../src/io/project-manifest-io";
import { projectVersionTxtPathFor } from "../../../src/io/project-version-io";
import { writeTextFile } from "../../../src/io/text-file-io";
Expand All @@ -16,7 +19,8 @@ const writeProjectManifest = WriteProjectManifestFile(writeTextFile);
* @returns The path to the created project's directory.
*/
export async function prepareUnityProject(
homeDirectory: string
homeDirectory: string,
options?: { manifest?: UnityProjectManifest }
): Promise<string> {
const projectName = "MyProject";
const projectDir = path.join(homeDirectory, projectName);
Expand All @@ -32,7 +36,10 @@ export async function prepareUnityProject(
{ encoding: "utf8" }
);

await writeProjectManifest(projectDir, emptyProjectManifest);
await writeProjectManifest(
projectDir,
options?.manifest ?? emptyProjectManifest
);

return projectDir;
}
67 changes: 0 additions & 67 deletions test/unit/cli/cmd-remove.test.ts

This file was deleted.

0 comments on commit e641666

Please sign in to comment.