From 432d9230c835075dd456d2e16a5a131ce3f6af24 Mon Sep 17 00:00:00 2001 From: Ramon Brullo Date: Sun, 14 Jan 2024 13:32:33 +0100 Subject: [PATCH] test: standardize unity project setup (#99) * refactor: extract helper function * feat: mock-project test-utility * refactor: use project-setup in add test * refactor: use project-setup in deps test * refactor: use project-setup in remove test * refactor: use project-setup in search test * refactor: use project-setup in view test * refactor: make project-setup async In preparation for async work in setup function. * feat: modify env for project-setup * feat: upmconfig for project-setup * refactor: use project-setup in env test * refactor: use project-setup in project-manifest test * refactor: move function * refactor: delete unused code --- src/utils/project-version-io.ts | 18 ++++ test/mock-work-dir.ts | 44 --------- test/setup/unity-project.ts | 153 +++++++++++++++++++++++++++++++ test/test-cmd-add.ts | 108 ++++++++++++---------- test/test-cmd-deps.ts | 19 ++-- test/test-cmd-remove.ts | 58 ++++++------ test/test-cmd-search.ts | 23 +++-- test/test-cmd-view.ts | 23 +++-- test/test-env.ts | 94 +++++++++---------- test/test-project-manifest-io.ts | 68 +++++++------- 10 files changed, 383 insertions(+), 225 deletions(-) create mode 100644 src/utils/project-version-io.ts delete mode 100644 test/mock-work-dir.ts create mode 100644 test/setup/unity-project.ts diff --git a/src/utils/project-version-io.ts b/src/utils/project-version-io.ts new file mode 100644 index 00000000..5cdb17a2 --- /dev/null +++ b/src/utils/project-version-io.ts @@ -0,0 +1,18 @@ +import path from "path"; +import fse from "fs-extra"; + +/** + * Creates a ProjectVersion.txt file for a Unity project. + * Nothing besides m_EditorVersion is specified. + * @param projectDirPath The projects root folder. + * @param version The editor-version to use. + */ +export function createProjectVersionTxt( + projectDirPath: string, + version: string +) { + const projectSettingsDir = path.join(projectDirPath, "ProjectSettings"); + fse.mkdirpSync(projectSettingsDir); + const data = `m_EditorVersion: ${version}`; + fse.writeFileSync(path.join(projectSettingsDir, "ProjectVersion.txt"), data); +} diff --git a/test/mock-work-dir.ts b/test/mock-work-dir.ts deleted file mode 100644 index d513e249..00000000 --- a/test/mock-work-dir.ts +++ /dev/null @@ -1,44 +0,0 @@ -import path from "path"; -import os from "os"; -import fse from "fs-extra"; -import _ from "lodash"; -import { - emptyProjectManifest, - UnityProjectManifest, -} from "../src/types/project-manifest"; - -export type ManifestCreationOptions = { - manifest: boolean | UnityProjectManifest; - editorVersion?: string; -}; -export const getWorkDir = function (pathToTmp: string): string { - return path.join(os.tmpdir(), pathToTmp); -}; -export const createWorkDir = function ( - pathToTmp: string, - { manifest, editorVersion }: ManifestCreationOptions -): string { - const workDir = getWorkDir(pathToTmp); - fse.mkdirpSync(workDir); - if (manifest) { - if (!_.isObjectLike(manifest)) manifest = emptyProjectManifest(); - const manifestDir = path.join(workDir, "Packages"); - fse.mkdirpSync(manifestDir); - const data = JSON.stringify(manifest); - fse.writeFileSync(path.join(manifestDir, "manifest.json"), data); - } - if (editorVersion) { - const projectSettingsDir = path.join(workDir, "ProjectSettings"); - fse.mkdirpSync(projectSettingsDir); - const data = `m_EditorVersion: ${editorVersion}`; - fse.writeFileSync( - path.join(projectSettingsDir, "ProjectVersion.txt"), - data - ); - } - return workDir; -}; -export const removeWorkDir = function (pathToTmp: string) { - const cwd = getWorkDir(pathToTmp); - fse.removeSync(cwd); -}; diff --git a/test/setup/unity-project.ts b/test/setup/unity-project.ts new file mode 100644 index 00000000..78482358 --- /dev/null +++ b/test/setup/unity-project.ts @@ -0,0 +1,153 @@ +import { + emptyProjectManifest, + UnityProjectManifest, +} from "../../src/types/project-manifest"; +import path from "path"; +import os from "os"; +import fse from "fs-extra"; +import { + loadProjectManifest, + saveProjectManifest, +} from "../../src/utils/project-manifest-io"; +import assert from "assert"; +import { mockEnv, MockEnvSession } from "../mock-env"; +import { UPMConfig } from "../../src/types/upm-config"; +import { saveUpmConfig } from "../../src/utils/upm-config-io"; +import {createProjectVersionTxt} from "../../src/utils/project-version-io"; + +/** + * A mock Unity project for testing + */ +export type MockUnityProject = { + /** + * The path to the projects root folder + */ + projectPath: string; + + /** + * Attempts to load the project manifest for the project. + * Null if not found. + */ + tryGetManifest(): UnityProjectManifest | null; + + /** + * Runs an assertion function on the project manifest. + * @param assertFn An assertion function. + * @throws AssertionError if no manifest was found. + */ + tryAssertManifest(assertFn: (manifest: UnityProjectManifest) => void): void; + + /** + * Resets the mock-project to its original state + */ + reset(): Promise; + + /** + * Deletes the mock-project + */ + restore(): Promise; +}; + +const defaultVersion = "2020.2.1f1"; + +const defaultManifest = emptyProjectManifest(); + +const defaultUpmConfig = {} satisfies UPMConfig; + +type Config = { + /** + * The version to use for the project. + * If not specified uses {@link defaultVersion} + */ + version?: string; + /** + * The manifest to use for the project. + * If not specified uses {@link defaultManifest}. + * If {@link false} no manifest is created + */ + manifest?: UnityProjectManifest | false; + + /** + * Override for the generated .upmconfig.toml. + * If not specified uses {@link defaultUpmConfig} + */ + upmConfig?: UPMConfig; +}; + +const rootPath = path.join(os.tmpdir(), "test-openupm-cli"); + +const projectPath = path.join(rootPath, "Project"); + +/** + * Setups a mock Unity project for testing. + * This will set up a directory structure with a Unity project, as well + * as some other effects: + * - Change {@link process.cwd} to {@link projectPath}. + * - Clear {@link process.env.USERPROFILE}. + * - Change {@link process.env.HOME} to {@link rootPath}. + * - Place a .upmconfig.toml in the root folder of the test directory structure. + * @param config Config describing the project to be setup + */ +export async function setupUnityProject( + config: Config +): Promise { + let originalCwd: () => string = null!; + let envSession: MockEnvSession = null!; + + async function setup() { + originalCwd = process.cwd; + process.cwd = () => projectPath; + + await fse.ensureDir(projectPath); + + envSession = mockEnv({ HOME: rootPath }); + + // Upmconfig + const upmConfig = config.upmConfig ?? defaultUpmConfig; + await saveUpmConfig(upmConfig, rootPath); + + // Editor-version + const version = config.version ?? defaultVersion; + createProjectVersionTxt(projectPath, version); + + // Project manifest + if (config.manifest !== false) { + const manifest = config.manifest ?? defaultManifest; + saveProjectManifest(projectPath, manifest); + } + } + + async function restore() { + process.cwd = originalCwd; + + await fse.rm(rootPath, { recursive: true, force: true }); + + envSession?.unhook(); + } + + function tryGetManifest(): UnityProjectManifest | null { + return loadProjectManifest(projectPath); + } + + function tryAssertManifest( + assertFn: (manifest: UnityProjectManifest) => void + ) { + const manifest = tryGetManifest(); + assert(manifest !== null); + assertFn(manifest); + } + + async function reset() { + await restore(); + await setup(); + } + + await setup(); + return { + projectPath, + tryGetManifest, + tryAssertManifest, + reset, + restore, + }; +} diff --git a/test/test-cmd-add.ts b/test/test-cmd-add.ts index 88f0cb5c..45da7bb3 100644 --- a/test/test-cmd-add.ts +++ b/test/test-cmd-add.ts @@ -8,21 +8,18 @@ import { startMockRegistry, stopMockRegistry, } from "./mock-registry"; -import { createWorkDir, getWorkDir, removeWorkDir } from "./mock-work-dir"; import { attachMockConsole, MockConsole } from "./mock-console"; -import { - shouldHaveDependency, - shouldHaveProjectManifest, -} from "./project-manifest-assertions"; +import { shouldHaveDependency } from "./project-manifest-assertions"; import { buildPackument } from "./data-packument"; import { buildProjectManifest } from "./data-project-manifest"; import { domainName } from "../src/types/domain-name"; import { PackageUrl } from "../src/types/package-url"; import { semanticVersion } from "../src/types/semantic-version"; import { packageReference } from "../src/types/package-reference"; +import { MockUnityProject, setupUnityProject } from "./setup/unity-project"; +import { after } from "mocha"; describe("cmd-add.ts", function () { - const workDirName = "test-openupm-cli"; const packageMissing = domainName("pkg-not-exist"); const packageA = domainName("com.base.package-a"); const packageB = domainName("com.base.package-b"); @@ -43,21 +40,18 @@ describe("cmd-add.ts", function () { _global: { registry: exampleRegistryUrl, upstream: false, - chdir: getWorkDir(workDirName), }, }; const upstreamOptions: AddOptions = { _global: { registry: exampleRegistryUrl, upstream: true, - chdir: getWorkDir(workDirName), }, }; const testableOptions: AddOptions = { _global: { registry: exampleRegistryUrl, upstream: false, - chdir: getWorkDir(workDirName), }, test: true, }; @@ -65,13 +59,12 @@ describe("cmd-add.ts", function () { _global: { registry: exampleRegistryUrl, upstream: false, - chdir: getWorkDir(workDirName), }, force: true, }; describe("add", function () { let mockConsole: MockConsole = null!; - let workDir = ""; + let mockProject: MockUnityProject = null!; const remotePackumentA = buildPackument(packageA, (packument) => packument.addVersion("0.1.0").addVersion("1.0.0") @@ -136,13 +129,11 @@ describe("cmd-add.ts", function () { manifest.addDependency(packageA, "1.0.0", true, true) ); - beforeEach(function () { - removeWorkDir(workDirName); - workDir = createWorkDir(workDirName, { - manifest: true, - editorVersion: "2019.2.13f1", - }); + before(async function () { + mockProject = await setupUnityProject({ version: "2019.2.13f1" }); + }); + beforeEach(function () { startMockRegistry(); registerRemotePackument(remotePackumentA); registerRemotePackument(remotePackumentB); @@ -156,17 +147,22 @@ describe("cmd-add.ts", function () { mockConsole = attachMockConsole(); }); - afterEach(function () { - removeWorkDir(workDirName); + afterEach(async function () { + await mockProject.reset(); stopMockRegistry(); mockConsole.detach(); }); + after(async function () { + await mockProject.restore(); + }); + it("add pkg", async function () { const retCode = await add(packageA, options); retCode.should.equal(0); - const manifest = shouldHaveProjectManifest(workDir); - manifest.should.deepEqual(expectedManifestA); + mockProject.tryAssertManifest((manifest) => + manifest.should.deepEqual(expectedManifestA) + ); mockConsole.hasLineIncluding("out", "added").should.be.ok(); mockConsole.hasLineIncluding("out", "open Unity").should.be.ok(); }); @@ -176,16 +172,18 @@ describe("cmd-add.ts", function () { options ); retCode.should.equal(0); - const manifest = shouldHaveProjectManifest(workDir); - manifest.should.deepEqual(expectedManifestA); + mockProject.tryAssertManifest((manifest) => + manifest.should.deepEqual(expectedManifestA) + ); mockConsole.hasLineIncluding("out", "added").should.be.ok(); mockConsole.hasLineIncluding("out", "open Unity").should.be.ok(); }); it("add pkg@latest", async function () { const retCode = await add(packageReference(packageA, "latest"), options); retCode.should.equal(0); - const manifest = shouldHaveProjectManifest(workDir); - manifest.should.deepEqual(expectedManifestA); + mockProject.tryAssertManifest((manifest) => + manifest.should.deepEqual(expectedManifestA) + ); mockConsole.hasLineIncluding("out", "added").should.be.ok(); mockConsole.hasLineIncluding("out", "open Unity").should.be.ok(); }); @@ -200,8 +198,9 @@ describe("cmd-add.ts", function () { options ); retCode2.should.equal(0); - const manifest = shouldHaveProjectManifest(workDir); - manifest.should.deepEqual(expectedManifestA); + mockProject.tryAssertManifest((manifest) => + manifest.should.deepEqual(expectedManifestA) + ); mockConsole.hasLineIncluding("out", "modified ").should.be.ok(); mockConsole.hasLineIncluding("out", "open Unity").should.be.ok(); }); @@ -216,8 +215,9 @@ describe("cmd-add.ts", function () { options ); retCode2.should.equal(0); - const manifest = shouldHaveProjectManifest(workDir); - manifest.should.deepEqual(expectedManifestA); + mockProject.tryAssertManifest((manifest) => + manifest.should.deepEqual(expectedManifestA) + ); mockConsole.hasLineIncluding("out", "existed ").should.be.ok(); mockConsole.hasLineIncluding("out", "open Unity").should.be.ok(); }); @@ -227,8 +227,9 @@ describe("cmd-add.ts", function () { options ); retCode.should.equal(1); - const manifest = shouldHaveProjectManifest(workDir); - manifest.should.deepEqual(defaultManifest); + mockProject.tryAssertManifest((manifest) => + manifest.should.deepEqual(defaultManifest) + ); mockConsole .hasLineIncluding("out", "version 2.0.0 is not a valid choice") .should.be.ok(); @@ -238,8 +239,9 @@ describe("cmd-add.ts", function () { const gitUrl = "https://github.com/yo/com.base.package-a" as PackageUrl; const retCode = await add(packageReference(packageA, gitUrl), options); retCode.should.equal(0); - const manifest = shouldHaveProjectManifest(workDir); - shouldHaveDependency(manifest, packageA, gitUrl); + mockProject.tryAssertManifest((manifest) => + shouldHaveDependency(manifest, packageA, gitUrl) + ); mockConsole.hasLineIncluding("out", "added").should.be.ok(); mockConsole.hasLineIncluding("out", "open Unity").should.be.ok(); }); @@ -247,8 +249,9 @@ describe("cmd-add.ts", function () { const gitUrl = "git@github.com:yo/com.base.package-a" as PackageUrl; const retCode = await add(packageReference(packageA, gitUrl), options); retCode.should.equal(0); - const manifest = shouldHaveProjectManifest(workDir); - shouldHaveDependency(manifest, packageA, gitUrl); + mockProject.tryAssertManifest((manifest) => + shouldHaveDependency(manifest, packageA, gitUrl) + ); mockConsole.hasLineIncluding("out", "added").should.be.ok(); mockConsole.hasLineIncluding("out", "open Unity").should.be.ok(); }); @@ -256,23 +259,26 @@ describe("cmd-add.ts", function () { const fileUrl = "file../yo/com.base.package-a" as PackageUrl; const retCode = await add(packageReference(packageA, fileUrl), options); retCode.should.equal(0); - const manifest = shouldHaveProjectManifest(workDir); - shouldHaveDependency(manifest, packageA, fileUrl); + mockProject.tryAssertManifest((manifest) => + shouldHaveDependency(manifest, packageA, fileUrl) + ); mockConsole.hasLineIncluding("out", "added").should.be.ok(); mockConsole.hasLineIncluding("out", "open Unity").should.be.ok(); }); it("add pkg-not-exist", async function () { const retCode = await add(packageMissing, options); retCode.should.equal(1); - const manifest = shouldHaveProjectManifest(workDir); - manifest.should.deepEqual(defaultManifest); + mockProject.tryAssertManifest((manifest) => + manifest.should.deepEqual(defaultManifest) + ); mockConsole.hasLineIncluding("out", "package not found").should.be.ok(); }); it("add more than one pkgs", async function () { const retCode = await add([packageA, packageB], options); retCode.should.equal(0); - const manifest = shouldHaveProjectManifest(workDir); - manifest.should.deepEqual(expectedManifestAB); + mockProject.tryAssertManifest((manifest) => + manifest.should.deepEqual(expectedManifestAB) + ); mockConsole .hasLineIncluding("out", "added com.base.package-a") .should.be.ok(); @@ -284,8 +290,9 @@ describe("cmd-add.ts", function () { it("add pkg from upstream", async function () { const retCode = await add(packageUp, upstreamOptions); retCode.should.equal(0); - const manifest = shouldHaveProjectManifest(workDir); - manifest.should.deepEqual(expectedManifestUpstream); + mockProject.tryAssertManifest((manifest) => + manifest.should.deepEqual(expectedManifestUpstream) + ); mockConsole .hasLineIncluding("out", "added com.upstream.package-up") .should.be.ok(); @@ -294,8 +301,9 @@ describe("cmd-add.ts", function () { it("add pkg-not-exist from upstream", async function () { const retCode = await add(packageMissing, upstreamOptions); retCode.should.equal(1); - const manifest = shouldHaveProjectManifest(workDir); - manifest.should.deepEqual(defaultManifest); + mockProject.tryAssertManifest((manifest) => + manifest.should.deepEqual(defaultManifest) + ); mockConsole.hasLineIncluding("out", "package not found").should.be.ok(); }); it("add pkg with nested dependencies", async function () { @@ -304,16 +312,18 @@ describe("cmd-add.ts", function () { upstreamOptions ); retCode.should.equal(0); - const manifest = shouldHaveProjectManifest(workDir); - manifest.should.deepEqual(expectedManifestC); + mockProject.tryAssertManifest((manifest) => + manifest.should.deepEqual(expectedManifestC) + ); mockConsole.hasLineIncluding("out", "added").should.be.ok(); mockConsole.hasLineIncluding("out", "open Unity").should.be.ok(); }); it("add pkg with tests", async function () { const retCode = await add(packageA, testableOptions); retCode.should.equal(0); - const manifest = shouldHaveProjectManifest(workDir); - manifest.should.deepEqual(expectedManifestTestable); + mockProject.tryAssertManifest((manifest) => + manifest.should.deepEqual(expectedManifestTestable) + ); mockConsole.hasLineIncluding("out", "added").should.be.ok(); mockConsole.hasLineIncluding("out", "open Unity").should.be.ok(); }); diff --git a/test/test-cmd-deps.ts b/test/test-cmd-deps.ts index 5bd87696..22089708 100644 --- a/test/test-cmd-deps.ts +++ b/test/test-cmd-deps.ts @@ -8,21 +8,22 @@ import { startMockRegistry, stopMockRegistry, } from "./mock-registry"; -import { createWorkDir, getWorkDir, removeWorkDir } from "./mock-work-dir"; import { attachMockConsole, MockConsole } from "./mock-console"; import { buildPackument } from "./data-packument"; import { domainName } from "../src/types/domain-name"; import { packageReference } from "../src/types/package-reference"; +import { MockUnityProject, setupUnityProject } from "./setup/unity-project"; +import { before } from "mocha"; describe("cmd-deps.ts", function () { const options: DepsOptions = { _global: { registry: exampleRegistryUrl, - chdir: getWorkDir("test-openupm-cli"), }, }; describe("deps", function () { let mockConsole: MockConsole = null!; + let mockProject: MockUnityProject = null!; const remotePackumentA = buildPackument( "com.example.package-a", @@ -43,9 +44,11 @@ describe("cmd-deps.ts", function () { (packument) => packument.addVersion("1.0.0") ); + before(async function () { + mockProject = await setupUnityProject({}); + }); + beforeEach(function () { - removeWorkDir("test-openupm-cli"); - createWorkDir("test-openupm-cli", { manifest: true }); startMockRegistry(); registerRemotePackument(remotePackumentA); registerRemotePackument(remotePackumentB); @@ -54,11 +57,15 @@ describe("cmd-deps.ts", function () { mockConsole = attachMockConsole(); }); - afterEach(function () { - removeWorkDir("test-openupm-cli"); + afterEach(async function () { + await mockProject.reset(); stopMockRegistry(); mockConsole.detach(); }); + after(async function () { + await mockProject.restore(); + }); + it("deps pkg", async function () { const retCode = await deps(remotePackumentA.name, options); retCode.should.equal(0); diff --git a/test/test-cmd-remove.ts b/test/test-cmd-remove.ts index ac1497da..c2e9edfc 100644 --- a/test/test-cmd-remove.ts +++ b/test/test-cmd-remove.ts @@ -1,10 +1,8 @@ import "should"; import { remove } from "../src/cmd-remove"; import { exampleRegistryUrl } from "./mock-registry"; -import { createWorkDir, removeWorkDir } from "./mock-work-dir"; import { attachMockConsole, MockConsole } from "./mock-console"; import { - shouldHaveProjectManifest, shouldHaveRegistryWithScopes, shouldNotHaveDependency, } from "./project-manifest-assertions"; @@ -12,46 +10,53 @@ import { buildProjectManifest } from "./data-project-manifest"; import { domainName } from "../src/types/domain-name"; import { semanticVersion } from "../src/types/semantic-version"; import { packageReference } from "../src/types/package-reference"; +import { MockUnityProject, setupUnityProject } from "./setup/unity-project"; +import { after } from "mocha"; const packageA = domainName("com.example.package-a"); const packageB = domainName("com.example.package-b"); const missingPackage = domainName("pkg-not-exist"); -const workDirName = "test-openupm-cli"; describe("cmd-remove.ts", function () { describe("remove", function () { - let mockConsole: MockConsole = null!; - let workDir = ""; - const defaultManifest = buildProjectManifest((manifest) => manifest .addDependency(packageA, "1.0.0", true, false) .addDependency(packageB, "1.0.0", true, false) ); + let mockConsole: MockConsole = null!; + let mockProject: MockUnityProject = null!; + + before(async function () { + mockProject = await setupUnityProject({ manifest: defaultManifest }); + }); + beforeEach(function () { - removeWorkDir(workDirName); - workDir = createWorkDir(workDirName, { - manifest: defaultManifest, - }); mockConsole = attachMockConsole(); }); - afterEach(function () { - removeWorkDir(workDirName); + + afterEach(async function () { + await mockProject.reset(); mockConsole.detach(); }); + + after(async function () { + await mockProject.restore(); + }); + it("remove pkg", async function () { const options = { _global: { registry: exampleRegistryUrl, - chdir: workDir, }, }; const retCode = await remove(packageA, options); retCode.should.equal(0); - const manifest = shouldHaveProjectManifest(workDir); - shouldNotHaveDependency(manifest, packageA); - shouldHaveRegistryWithScopes(manifest, [packageB]); + mockProject.tryAssertManifest((manifest) => { + shouldNotHaveDependency(manifest, packageA); + shouldHaveRegistryWithScopes(manifest, [packageB]); + }); mockConsole.hasLineIncluding("out", "removed ").should.be.ok(); mockConsole.hasLineIncluding("out", "open Unity").should.be.ok(); }); @@ -59,7 +64,6 @@ describe("cmd-remove.ts", function () { const options = { _global: { registry: exampleRegistryUrl, - chdir: workDir, }, }; const retCode = await remove( @@ -67,35 +71,37 @@ describe("cmd-remove.ts", function () { options ); retCode.should.equal(1); - const manifest = shouldHaveProjectManifest(workDir); - manifest.should.deepEqual(defaultManifest); + mockProject.tryAssertManifest((manifest) => { + manifest.should.deepEqual(defaultManifest); + }); mockConsole.hasLineIncluding("out", "please replace").should.be.ok(); }); it("remove pkg-not-exist", async function () { const options = { _global: { registry: exampleRegistryUrl, - chdir: workDir, }, }; const retCode = await remove(missingPackage, options); retCode.should.equal(1); - const manifest = shouldHaveProjectManifest(workDir); - manifest.should.deepEqual(defaultManifest); + mockProject.tryAssertManifest((manifest) => { + manifest.should.deepEqual(defaultManifest); + }); mockConsole.hasLineIncluding("out", "package not found").should.be.ok(); }); it("remove more than one pkgs", async function () { const options = { _global: { registry: exampleRegistryUrl, - chdir: workDir, }, }; const retCode = await remove([packageA, packageB], options); retCode.should.equal(0); - const manifest = shouldHaveProjectManifest(workDir); - shouldNotHaveDependency(manifest, packageA); - shouldNotHaveDependency(manifest, packageB); + + mockProject.tryAssertManifest((manifest) => { + shouldNotHaveDependency(manifest, packageA); + shouldNotHaveDependency(manifest, packageB); + }); mockConsole .hasLineIncluding("out", "removed com.example.package-a") .should.be.ok(); diff --git a/test/test-cmd-search.ts b/test/test-cmd-search.ts index 6db3287d..bef7b87c 100644 --- a/test/test-cmd-search.ts +++ b/test/test-cmd-search.ts @@ -8,31 +8,40 @@ import { stopMockRegistry, } from "./mock-registry"; import { SearchEndpointResult } from "./types"; -import { createWorkDir, getWorkDir, removeWorkDir } from "./mock-work-dir"; import { attachMockConsole, MockConsole } from "./mock-console"; import { domainName } from "../src/types/domain-name"; import { semanticVersion } from "../src/types/semantic-version"; +import { MockUnityProject, setupUnityProject } from "./setup/unity-project"; +import { before } from "mocha"; describe("cmd-search.ts", function () { let mockConsole: MockConsole = null!; + let mockProject: MockUnityProject = null!; const options: SearchOptions = { _global: { registry: exampleRegistryUrl, upstream: false, - chdir: getWorkDir("test-openupm-cli"), }, }; - getWorkDir("test-openupm-cli"); + + before(async function () { + mockProject = await setupUnityProject({}); + }); + beforeEach(function () { - removeWorkDir("test-openupm-cli"); - createWorkDir("test-openupm-cli", { manifest: true }); mockConsole = attachMockConsole(); }); - afterEach(function () { - removeWorkDir("test-openupm-cli"); + + afterEach(async function () { + await mockProject.reset(); mockConsole.detach(); }); + + after(async function () { + await mockProject.restore(); + }); + describe("search endpoint", function () { const searchEndpointResult: SearchEndpointResult = { objects: [ diff --git a/test/test-cmd-view.ts b/test/test-cmd-view.ts index b70cc272..0f8c5104 100644 --- a/test/test-cmd-view.ts +++ b/test/test-cmd-view.ts @@ -8,12 +8,13 @@ import { startMockRegistry, stopMockRegistry, } from "./mock-registry"; -import { createWorkDir, getWorkDir, removeWorkDir } from "./mock-work-dir"; import { attachMockConsole, MockConsole } from "./mock-console"; import { buildPackument } from "./data-packument"; import { domainName } from "../src/types/domain-name"; import { semanticVersion } from "../src/types/semantic-version"; import { packageReference } from "../src/types/package-reference"; +import { MockUnityProject, setupUnityProject } from "./setup/unity-project"; +import { after, before } from "mocha"; const packageA = domainName("com.example.package-a"); const packageUp = domainName("com.example.package-up"); @@ -22,19 +23,20 @@ const packageMissing = domainName("pkg-not-exist"); describe("cmd-view.ts", function () { const options: ViewOptions = { _global: { + color: false, registry: exampleRegistryUrl, upstream: false, - chdir: getWorkDir("test-openupm-cli"), }, }; const upstreamOptions: ViewOptions = { _global: { + color: false, registry: exampleRegistryUrl, - chdir: getWorkDir("test-openupm-cli"), }, }; describe("view", function () { let mockConsole: MockConsole = null!; + let mockProject: MockUnityProject = null!; const remotePackumentA = buildPackument(packageA, (packument) => packument @@ -102,20 +104,27 @@ describe("cmd-view.ts", function () { ) ); + before(async function () { + mockProject = await setupUnityProject({}); + }); + beforeEach(function () { - removeWorkDir("test-openupm-cli"); - createWorkDir("test-openupm-cli", { manifest: true }); startMockRegistry(); registerRemotePackument(remotePackumentA); registerMissingPackument(packageMissing); registerRemoteUpstreamPackument(remotePackumentUp); mockConsole = attachMockConsole(); }); - afterEach(function () { - removeWorkDir("test-openupm-cli"); + afterEach(async function () { + await mockProject.reset(); stopMockRegistry(); mockConsole.detach(); }); + + after(async function () { + await mockProject.restore(); + }); + it("view pkg", async function () { const retCode = await view(packageA, options); retCode.should.equal(0); diff --git a/test/test-env.ts b/test/test-env.ts index 87fcf3de..8b46c0d3 100644 --- a/test/test-env.ts +++ b/test/test-env.ts @@ -1,13 +1,15 @@ import "should"; import { parseEnv } from "../src/utils/env"; -import { createWorkDir, getWorkDir, removeWorkDir } from "./mock-work-dir"; import { attachMockConsole, MockConsole } from "./mock-console"; import should from "should"; -import { runWithEnv } from "./mock-env"; import { saveUpmConfig } from "../src/utils/upm-config-io"; import { registryUrl } from "../src/types/registry-url"; import { TokenAuth, UPMConfig } from "../src/types/upm-config"; import { NpmAuth } from "another-npm-registry-client"; +import { MockUnityProject, setupUnityProject } from "./setup/unity-project"; +import { afterEach, before } from "mocha"; +import { manifestPathFor } from "../src/types/project-manifest"; +import fse from "fs-extra"; const testUpmAuth: TokenAuth = { email: "test@mail.com", @@ -26,29 +28,28 @@ const testUpmConfig: UPMConfig = { describe("env", function () { describe("parseEnv", function () { let mockConsole: MockConsole = null!; - before(function () { - removeWorkDir("test-openupm-cli"); - removeWorkDir("test-openupm-cli-no-manifest"); - createWorkDir("test-openupm-cli", { - manifest: true, - editorVersion: " 2019.2.13f1", - }); - createWorkDir("test-openupm-cli-no-manifest", { - manifest: false, - editorVersion: " 2019.2.13f1", + let mockProject: MockUnityProject = null!; + + before(async function () { + mockProject = await setupUnityProject({ + version: "2019.2.13f1", + upmConfig: testUpmConfig, }); - saveUpmConfig(testUpmConfig, getWorkDir("test-openupm-cli")); - }); - after(function () { - removeWorkDir("test-openupm-cli"); - removeWorkDir("test-openupm-cli-no-manifest"); }); + beforeEach(function () { mockConsole = attachMockConsole(); }); - afterEach(function () { + + afterEach(async function () { mockConsole.detach(); + await mockProject.reset(); + }); + + after(async function () { + await mockProject.restore(); }); + it("defaults", async function () { const env = await parseEnv({ _global: {} }, false); should(env).not.be.null(); @@ -58,17 +59,15 @@ describe("env", function () { env!.cwd.should.equal(""); (env!.editorVersion === null).should.be.ok(); }); + it("check path", async function () { - const env = await parseEnv( - { _global: { chdir: getWorkDir("test-openupm-cli") } }, - true - ); + const env = await parseEnv({ _global: {} }, true); should(env).not.be.null(); - env!.cwd.should.be.equal(getWorkDir("test-openupm-cli")); + env!.cwd.should.be.equal(mockProject.projectPath); }); it("can not resolve path", async function () { const env = await parseEnv( - { _global: { chdir: getWorkDir("path-not-exist") } }, + { _global: { chdir: "/path-not-exist" } }, true ); should(env).be.null(); @@ -76,11 +75,13 @@ describe("env", function () { .hasLineIncluding("out", "can not resolve path") .should.be.ok(); }); + it("can not locate manifest.json", async function () { - const env = await parseEnv( - { _global: { chdir: getWorkDir("test-openupm-cli-no-manifest") } }, - true - ); + // Delete manifest + const manifestPath = manifestPathFor(mockProject.projectPath); + fse.rmSync(manifestPath); + + const env = await parseEnv({ _global: {} }, true); should(env).be.null(); mockConsole .hasLineIncluding("out", "can not locate manifest.json") @@ -153,33 +154,25 @@ describe("env", function () { env!.registry.url.should.be.equal("http://[1:2:3:4:5:6:7:8]:4873"); }); it("should have registry auth if specified", async function () { - const projectDir = getWorkDir("test-openupm-cli"); - const env = await runWithEnv({ HOME: projectDir }, () => - parseEnv( - { - _global: { - registry: "registry.npmjs.org", - chdir: projectDir, - }, + const env = await parseEnv( + { + _global: { + registry: "registry.npmjs.org", }, - true - ) + }, + true ); should(env).not.be.null(); should(env!.registry.auth).deepEqual(testNpmAuth); }); it("should not have unspecified registry auth", async function () { - const projectDir = getWorkDir("test-openupm-cli"); - const env = await runWithEnv({ HOME: projectDir }, () => - parseEnv( - { - _global: { - registry: "registry.other.org", - chdir: projectDir, - }, + const env = await parseEnv( + { + _global: { + registry: "registry.other.org", }, - true - ) + }, + true ); should(env).not.be.null(); should(env!.registry.auth).be.null(); @@ -190,10 +183,7 @@ describe("env", function () { env!.upstream.should.not.be.ok(); }); it("editorVersion", async function () { - const env = await parseEnv( - { _global: { chdir: getWorkDir("test-openupm-cli") } }, - true - ); + const env = await parseEnv({ _global: {} }, true); should(env).not.be.null(); should(env!.editorVersion).be.equal("2019.2.13f1"); }); diff --git a/test/test-project-manifest-io.ts b/test/test-project-manifest-io.ts index 20680d66..d8955984 100644 --- a/test/test-project-manifest-io.ts +++ b/test/test-project-manifest-io.ts @@ -1,66 +1,66 @@ import { attachMockConsole, MockConsole } from "./mock-console"; -import fs from "fs"; import "should"; -import { saveProjectManifest } from "../src/utils/project-manifest-io"; -import { describe } from "mocha"; -import { createWorkDir, getWorkDir, removeWorkDir } from "./mock-work-dir"; import { - shouldHaveProjectManifest, - shouldHaveNoProjectManifest, - shouldNotHaveAnyDependencies, -} from "./project-manifest-assertions"; + loadProjectManifest, + saveProjectManifest, +} from "../src/utils/project-manifest-io"; +import { describe } from "mocha"; +import { shouldNotHaveAnyDependencies } from "./project-manifest-assertions"; import { domainName } from "../src/types/domain-name"; import { semanticVersion } from "../src/types/semantic-version"; import { addDependency, manifestPathFor } from "../src/types/project-manifest"; import should from "should"; - -const workDirName = "test-openupm-cli"; -const wrongJsonWorkDirName = "test-openupm-cli-wrong-json"; +import { MockUnityProject, setupUnityProject } from "./setup/unity-project"; +import fse from "fs-extra"; describe("project-manifest io", function () { let mockConsole: MockConsole = null!; - let workDir = ""; - let wrongJsonWorkDir = ""; + let mockProject: MockUnityProject = null!; + + before(async function () { + mockProject = await setupUnityProject({}); + }); beforeEach(function () { - removeWorkDir(workDirName); - workDir = createWorkDir(workDirName, { manifest: true }); - wrongJsonWorkDir = createWorkDir(wrongJsonWorkDirName, { - manifest: true, - }); - fs.writeFileSync( - manifestPathFor(getWorkDir(wrongJsonWorkDirName)), - "wrong-json" - ); mockConsole = attachMockConsole(); }); - afterEach(function () { - removeWorkDir(workDirName); - removeWorkDir(wrongJsonWorkDirName); + + afterEach(async function () { + await mockProject.reset(); mockConsole.detach(); }); + + after(async function () { + await mockProject.restore(); + }); + it("loadManifest", async function () { - const manifest = shouldHaveProjectManifest(workDir); - manifest.should.be.deepEqual({ dependencies: {} }); + const manifest = loadProjectManifest(mockProject.projectPath); + should(manifest).be.deepEqual({ dependencies: {} }); }); it("no manifest file", async function () { - shouldHaveNoProjectManifest("path-not-exist"); + const manifest = loadProjectManifest("/invalid-path"); + should(manifest).be.null(); mockConsole.hasLineIncluding("out", "does not exist").should.be.ok(); }); it("wrong json content", async function () { - shouldHaveNoProjectManifest(wrongJsonWorkDir); + const manifestPath = manifestPathFor(mockProject.projectPath); + fse.writeFileSync(manifestPath, "invalid data"); + + const manifest = loadProjectManifest(mockProject.projectPath); + should(manifest).be.null(); mockConsole.hasLineIncluding("out", "failed to parse").should.be.ok(); }); it("saveManifest", async function () { - const manifest = shouldHaveProjectManifest(workDir); + const manifest = loadProjectManifest(mockProject.projectPath)!; shouldNotHaveAnyDependencies(manifest); addDependency(manifest, domainName("some-pack"), semanticVersion("1.0.0")); - saveProjectManifest(workDir, manifest).should.be.ok(); - const manifest2 = shouldHaveProjectManifest(workDir); - manifest2.should.be.deepEqual(manifest); + saveProjectManifest(mockProject.projectPath, manifest).should.be.ok(); + const manifest2 = loadProjectManifest(mockProject.projectPath); + should(manifest2).be.deepEqual(manifest); }); it("manifest-path is correct", function () { - const manifestPath = manifestPathFor(workDirName); + const manifestPath = manifestPathFor("test-openupm-cli"); should(manifestPath).be.equal("test-openupm-cli/Packages/manifest.json"); }); });