From 1680575fc746d16e64c71c4d4001ae849d7c6538 Mon Sep 17 00:00:00 2001 From: Noah Sherwin Date: Thu, 24 Oct 2024 21:35:22 -0700 Subject: [PATCH] style(dotnet): format tests --- tests/commitlintConfig.test.ts | 126 +++--- tests/dotnet/GithubNugetRegistryInfo.test.ts | 38 +- tests/dotnet/GitlabNugetRegistryInfo.test.ts | 400 +++++++++---------- tests/envUtils.test.ts | 44 +- tests/eslintConfig.test.ts | 18 +- tests/index.test.ts | 54 +-- tests/package.test.ts | 336 ++++++++-------- tests/setupGitPluginSpec.test.ts | 32 +- 8 files changed, 524 insertions(+), 524 deletions(-) diff --git a/tests/commitlintConfig.test.ts b/tests/commitlintConfig.test.ts index cd6bf70a..a6d51bef 100644 --- a/tests/commitlintConfig.test.ts +++ b/tests/commitlintConfig.test.ts @@ -1,67 +1,67 @@ -import { deepStrictEqual, ok } from 'node:assert'; -import { describe, it } from 'node:test'; -import commitLintConventional from '@commitlint/config-conventional'; -import load from '@commitlint/load'; -import { RuleConfigSeverity, type QualifiedRuleConfig } from '@commitlint/types'; -import rawConfig from '@halospv3/hce.shared-config/commitlintConfig'; +import { deepStrictEqual, ok } from 'node:assert' +import { describe, it } from 'node:test' +import commitLintConventional from '@commitlint/config-conventional' +import load from '@commitlint/load' +import { RuleConfigSeverity, type QualifiedRuleConfig } from '@commitlint/types' +import rawConfig from '@halospv3/hce.shared-config/commitlintConfig' // @ts-expect-error ts(7016) Could not find a declaration file -import createPreset from 'conventional-changelog-conventionalcommits'; +import createPreset from 'conventional-changelog-conventionalcommits' -await describe("commitlintConfig", async () => { - await describe("rawConfig", async () => { - await it("has extends", () => { - if (typeof rawConfig.extends === 'string') - deepStrictEqual(rawConfig.extends, '@commitlint/config-conventional'); - else if (Array.isArray(rawConfig.extends)) - deepStrictEqual(rawConfig.extends, ['@commitlint/config-conventional']); - else ok(false, 'extends is not defined in config file or is an unsupported type!'); - }); +await describe('commitlintConfig', async () => { + await describe('rawConfig', async () => { + await it('has extends', () => { + if (typeof rawConfig.extends === 'string') + deepStrictEqual(rawConfig.extends, '@commitlint/config-conventional') + else if (Array.isArray(rawConfig.extends)) + deepStrictEqual(rawConfig.extends, ['@commitlint/config-conventional']) + else ok(false, 'extends is not defined in config file or is an unsupported type!') + }) - await it("has rules", () => { - ok(rawConfig.rules); - const rules = rawConfig.rules; - deepStrictEqual( - rules['body-max-line-length'], - [RuleConfigSeverity.Disabled] as QualifiedRuleConfig - ); - deepStrictEqual( - rules['footer-max-line-length'], - [RuleConfigSeverity.Disabled] as QualifiedRuleConfig - ); - deepStrictEqual( - rules['header-max-length'], - [RuleConfigSeverity.Disabled] as QualifiedRuleConfig - ); - }); - }); + await it('has rules', () => { + ok(rawConfig.rules) + const rules = rawConfig.rules + deepStrictEqual( + rules['body-max-line-length'], + [RuleConfigSeverity.Disabled] as QualifiedRuleConfig, + ) + deepStrictEqual( + rules['footer-max-line-length'], + [RuleConfigSeverity.Disabled] as QualifiedRuleConfig, + ) + deepStrictEqual( + rules['header-max-length'], + [RuleConfigSeverity.Disabled] as QualifiedRuleConfig, + ) + }) + }) - await describe('Loaded config tests', async () => { - const config = await load(rawConfig); - await it('has parserPreset', async () => { - const preset = await createPreset(); - ok(preset); - ok("parserOpts" in preset || "parser" in preset) - ok(config.parserPreset); - deepStrictEqual(config.parserPreset.name, commitLintConventional.parserPreset); - if ("parser" in preset) - deepStrictEqual(config.parserPreset.parserOpts, preset.parser); - else { - deepStrictEqual(config.parserPreset.parserOpts, preset.parserOpts) - } - }); - await it('has rules', async () => { - deepStrictEqual(config.rules['body-leading-blank'], commitLintConventional.rules['body-leading-blank']); - deepStrictEqual(config.rules['body-max-line-length']?.[0], RuleConfigSeverity.Disabled); - deepStrictEqual(config.rules['footer-leading-blank'], commitLintConventional.rules['footer-leading-blank']); - deepStrictEqual(config.rules['footer-max-line-length']?.[0], RuleConfigSeverity.Disabled); - deepStrictEqual(config.rules['header-max-length']?.[0], RuleConfigSeverity.Disabled); - deepStrictEqual(config.rules['header-trim'], commitLintConventional.rules['header-trim']); - deepStrictEqual(config.rules['subject-case'], commitLintConventional.rules['subject-case']); - deepStrictEqual(config.rules['subject-empty'], commitLintConventional.rules['subject-empty']); - deepStrictEqual(config.rules['subject-full-stop'], commitLintConventional.rules['subject-full-stop']); - deepStrictEqual(config.rules['type-case'], commitLintConventional.rules['type-case']); - deepStrictEqual(config.rules['type-empty'], commitLintConventional.rules['type-empty']); - deepStrictEqual(config.rules['type-enum'], commitLintConventional.rules['type-enum']); - }); - }); -}) \ No newline at end of file + await describe('Loaded config tests', async () => { + const config = await load(rawConfig) + await it('has parserPreset', async () => { + const preset = await createPreset() + ok(preset) + ok('parserOpts' in preset || 'parser' in preset) + ok(config.parserPreset) + deepStrictEqual(config.parserPreset.name, commitLintConventional.parserPreset) + if ('parser' in preset) + deepStrictEqual(config.parserPreset.parserOpts, preset.parser) + else { + deepStrictEqual(config.parserPreset.parserOpts, preset.parserOpts) + } + }) + await it('has rules', async () => { + deepStrictEqual(config.rules['body-leading-blank'], commitLintConventional.rules['body-leading-blank']) + deepStrictEqual(config.rules['body-max-line-length']?.[0], RuleConfigSeverity.Disabled) + deepStrictEqual(config.rules['footer-leading-blank'], commitLintConventional.rules['footer-leading-blank']) + deepStrictEqual(config.rules['footer-max-line-length']?.[0], RuleConfigSeverity.Disabled) + deepStrictEqual(config.rules['header-max-length']?.[0], RuleConfigSeverity.Disabled) + deepStrictEqual(config.rules['header-trim'], commitLintConventional.rules['header-trim']) + deepStrictEqual(config.rules['subject-case'], commitLintConventional.rules['subject-case']) + deepStrictEqual(config.rules['subject-empty'], commitLintConventional.rules['subject-empty']) + deepStrictEqual(config.rules['subject-full-stop'], commitLintConventional.rules['subject-full-stop']) + deepStrictEqual(config.rules['type-case'], commitLintConventional.rules['type-case']) + deepStrictEqual(config.rules['type-empty'], commitLintConventional.rules['type-empty']) + deepStrictEqual(config.rules['type-enum'], commitLintConventional.rules['type-enum']) + }) + }) +}) diff --git a/tests/dotnet/GithubNugetRegistryInfo.test.ts b/tests/dotnet/GithubNugetRegistryInfo.test.ts index c7061dc1..5d03474c 100644 --- a/tests/dotnet/GithubNugetRegistryInfo.test.ts +++ b/tests/dotnet/GithubNugetRegistryInfo.test.ts @@ -1,13 +1,13 @@ -import { GithubNugetRegistryInfo } from '@halospv3/hce.shared-config/dotnet/GithubNugetRegistryInfo'; -import { getEnv, getEnvVarValue } from '@halospv3/hce.shared-config/envUtils'; -import { type DotenvConfigOptions } from 'dotenv'; -import { notDeepStrictEqual, ok, strictEqual } from 'node:assert'; -import { existsSync, writeFileSync } from 'node:fs'; -import { dirname, join } from 'node:path'; -import { env } from 'node:process'; -import { describe, it } from 'node:test'; +import { GithubNugetRegistryInfo } from '@halospv3/hce.shared-config/dotnet/GithubNugetRegistryInfo' +import { getEnv, getEnvVarValue } from '@halospv3/hce.shared-config/envUtils' +import { type DotenvConfigOptions } from 'dotenv' +import { notDeepStrictEqual, ok, strictEqual } from 'node:assert' +import { existsSync, writeFileSync } from 'node:fs' +import { dirname, join } from 'node:path' +import { env } from 'node:process' +import { describe, it } from 'node:test' -const dotenvPath = join(dirname(dirname(import.meta.dirname)), '.env'); +const dotenvPath = join(dirname(dirname(import.meta.dirname)), '.env') if (!existsSync(dotenvPath)) writeFileSync(dotenvPath, '') const dotenvOptions: DotenvConfigOptions = { path: dotenvPath } @@ -17,31 +17,31 @@ const dotenvOptions: DotenvConfigOptions = { path: dotenvPath } * @returns the value of env.GITHUB_REPOSITORY_OWNER */ function getOwner(): string { - return env.GITHUB_REPOSITORY_OWNER ??= "HaloSPV3"; + return env.GITHUB_REPOSITORY_OWNER ??= 'HaloSPV3' } await describe('GithubNugetRegistryInfo', async () => { await describe('canPushPackagesToUrl', async () => { await it('returns true when GITHUB_TOKEN is valid and GITHUB_REPOSITORY_OWNER is defined', async (t) => { if (!getEnvVarValue('GITHUB_TOKEN')) - return t.skip('GITHUB_TOKEN is unavailable for testing'); + return t.skip('GITHUB_TOKEN is unavailable for testing') if (getEnvVarValue('CI') && !(getEnvVarValue('GITHUB_ACTION')?.includes('release'))) return t.skip() - getOwner(); + getOwner() strictEqual(await new GithubNugetRegistryInfo().canPushPackagesToUrl, true) }) await it('throws when GITHUB_TOKEN is invalid', async () => { - getOwner(); - const tokenEnvVar = 'TOKEN_CANNOT_WRITE'; + getOwner() + const tokenEnvVar = 'TOKEN_CANNOT_WRITE' getEnv(undefined, { TOKEN_CANNOT_WRITE: tokenEnvVar }) - const result = await new GithubNugetRegistryInfo(tokenEnvVar).canPushPackagesToUrl.catch(reason => reason instanceof Error ? reason : new Error(String(reason))); - notDeepStrictEqual(result, true); - ok(result instanceof Error); - }); - }); + const result = await new GithubNugetRegistryInfo(tokenEnvVar).canPushPackagesToUrl.catch(reason => reason instanceof Error ? reason : new Error(String(reason))) + notDeepStrictEqual(result, true) + ok(result instanceof Error) + }) + }) // todo: refactor to test new NugetRegistryInfo methods /* await describe('toRegistryPair', async () => { diff --git a/tests/dotnet/GitlabNugetRegistryInfo.test.ts b/tests/dotnet/GitlabNugetRegistryInfo.test.ts index c50911bd..ee411b3a 100644 --- a/tests/dotnet/GitlabNugetRegistryInfo.test.ts +++ b/tests/dotnet/GitlabNugetRegistryInfo.test.ts @@ -1,201 +1,199 @@ -import { describe, it, todo } from 'node:test'; -import { ok, strictEqual } from 'node:assert/strict'; -import { GitlabNugetRegistryInfo } from '@halospv3/hce.shared-config/dotnet/GitlabNugetRegistryInfo'; -import { getEnv, getEnvVarValue } from '@halospv3/hce.shared-config/envUtils'; - -await describe("GitlabNugetRegistryInfo", async (ctx0) => { - await it("has expected name", async () => { - strictEqual(GitlabNugetRegistryInfo.name, ctx0.name) - }); - - await describe("an instance of GitlabNugetRegistryInfo", async (ctx1) => { - ok(ctx1); - - if (!getEnvVarValue("CI_JOB_TOKEN")) - process.env.CI_JOB_TOKEN = "placeholder"; - if (!getEnvVarValue("CI_PROJECT_ID")) - process.env.CI_PROJECT_ID = "placeholder" - const defaultWithPlaceholders = new GitlabNugetRegistryInfo(); - - await it("defaults to project-level endpoint", async () => { - if (!getEnvVarValue("CI_PROJECT_ID")) - process.env.CI_PROJECT_ID = "placeholder"; - if (!getEnvVarValue("CI_JOB_TOKEN")) - process.env.CI_JOB_TOKEN = "placeholder"; - const expected = `${GitlabNugetRegistryInfo.CI_API_V4_URL}/projects/${GitlabNugetRegistryInfo.projectId}/packages/nuget/index.json`; - strictEqual(new GitlabNugetRegistryInfo().url, expected); - }); - - await it("can be configured to use group-level endpoint", async () => { - if (!getEnvVarValue("CI_PROJECT_NAMESPACE_ID")) - process.env.CI_PROJECT_NAMESPACE_ID = "placeholder" - if (!getEnvVarValue("CI_JOB_TOKEN")) - process.env.CI_JOB_TOKEN = "placeholder"; - const expected = `${GitlabNugetRegistryInfo.CI_API_V4_URL}/groups/${GitlabNugetRegistryInfo.ownerId}/-/packages/nuget/index.json`; - strictEqual( - new GitlabNugetRegistryInfo(undefined, undefined, undefined, true).url, - expected - ); - }); - - await it("throws if default values and no token available", async (t) => { - const { CI_JOB_TOKEN, GL_TOKEN, GITLAB_TOKEN } = getEnv(); - delete process.env.CI_JOB_TOKEN; - delete process.env.GL_TOKEN; - delete process.env.GITLAB_TOKEN; - if (getEnvVarValue("CI_JOB_TOKEN") || getEnvVarValue("GL_TOKEN") || getEnvVarValue("GITLAB_TOKEN")) - return t.skip("one or more tokens (CI_JOB_TOKEN, GL_TOKEN, GITLAB_TOKEN) is defined in .env file") - - let value: GitlabNugetRegistryInfo | Error; - try { - value = new GitlabNugetRegistryInfo() - } - catch (err) { - value = err instanceof Error ? err : new Error(String(err)); - } - - ok(value instanceof Error) - - if (CI_JOB_TOKEN) - process.env.CI_JOB_TOKEN = CI_JOB_TOKEN; - if (GL_TOKEN) - process.env.GL_TOKEN = GL_TOKEN; - if (GITLAB_TOKEN) - process.env.GITLAB_TOKEN = GITLAB_TOKEN; - }); - - await it("throws when custom values and no token available", async () => { - let value: GitlabNugetRegistryInfo | Error; - try { - value = new GitlabNugetRegistryInfo(undefined, "UNDEFINED_TOKEN", ["ANOTHER_UNDEFINED_TOKEN"]); - } - catch (err) { - value = err instanceof Error ? err : new Error(String(err)); - } - ok(value instanceof Error); - }); - - await describe("canPushPackagesToUrl", async (ctx2) => { - await it("has expected name", async () => { - ok(ctx2.name in defaultWithPlaceholders) - }); - - await todo("can...uhhhh...Sorry. Brainrot.") - }); - - await describe("resolvedEnvVariable", async (ctx2) => { - await it("has expected name", async () => { - ok(ctx2.name in defaultWithPlaceholders); - }); - - await it("is a string", async () => { - strictEqual(typeof defaultWithPlaceholders.resolvedEnvVariable, "string"); - }); - }); - - /* await describe("toRegistryPair", async (ctx2) => { - await it("has expected name", async () => { - ok(ctx2.name in defaultWithPlaceholders); - }); - }); */ - - await describe("url", async (ctx2) => { - await it("has expected name", async () => { - ok(ctx2.name in defaultWithPlaceholders); - }); - - await it("is a string", async () => { - strictEqual(typeof defaultWithPlaceholders.url, "string"); - }); - }); - - /** - * value.toRegistryPair - * value.url - */ - }); - - await describe("CI_API_V4_URL", async (ctx1) => { - const expectedValue = "https://gitlab.com/api/v4"; - - await it("has expected name", async () => { - ok(ctx1.name in GitlabNugetRegistryInfo); - }); - - await it("has the correct default value if the environment variable is undefined", async () => { - delete process.env.CI_API_V4_URL; - strictEqual(GitlabNugetRegistryInfo.CI_API_V4_URL, expectedValue); - }); - - // await it("has the correct value the value is provided by the environment variable") - }); - - await describe("groupUrl", async (ctx1) => { - await it("has expected name", async () => { - ok(ctx1.name in GitlabNugetRegistryInfo) - }); - - await it("returns the expected url when CI_PROJECT_NAMESPACE_ID is defined", async () => { - process.env.CI_PROJECT_NAMESPACE_ID = "placeholder"; - strictEqual( - GitlabNugetRegistryInfo.groupUrl, - `${GitlabNugetRegistryInfo.CI_API_V4_URL}/groups/${GitlabNugetRegistryInfo.ownerId}/-/packages/nuget/index.json` - ); - }); - }); - - await describe("projectUrl", async (ctx1) => { - await it("has expected name", async () => { - ok(ctx1.name in GitlabNugetRegistryInfo) - }); - - await it("returns the expected url when CI_PROJECT_ID is defined", async () => { - if (!getEnvVarValue("CI_PROJECT_ID")) - process.env.CI_PROJECT_ID = "placeholder" - strictEqual( - GitlabNugetRegistryInfo.projectUrl, - `${GitlabNugetRegistryInfo.CI_API_V4_URL}/projects/${GitlabNugetRegistryInfo.projectId}/packages/nuget/index.json` - ) - }); - }); - - await describe("ownerId", async (ctx1) => { - await it("has expected name", async () => { - ok(ctx1.name in GitlabNugetRegistryInfo) - }); - - await it("returns undefined if CI_PROJECT_NAMESPACE_ID is undefined", async (t) => { - delete process.env.CI_PROJECT_NAMESPACE_ID; - if (getEnvVarValue("CI_PROJECT_NAMESPACE_ID")) - return t.skip("This test requires CI_PROJECT_NAMESPACE_ID be undefined. It is defined in .env file and so it is too annoying to work around."); - strictEqual(GitlabNugetRegistryInfo.ownerId, undefined) - }); - - await it("returns string if CI_PROJECT_NAMESPACE_ID is defined", async () => { - if (!getEnvVarValue("CI_PROJECT_NAMESPACE_ID")) - process.env.CI_PROJECT_NAMESPACE_ID = "placeholder"; - strictEqual(GitlabNugetRegistryInfo.ownerId, process.env.CI_PROJECT_NAMESPACE_ID); - }); - }); - - - await describe("projectId", async (ctx1) => { - await it("has expected name", async () => { - ok(ctx1.name in GitlabNugetRegistryInfo); - }); - - await it("returns undefined if CI_PROJECT_ID is undefined", async (t) => { - delete process.env.CI_PROJECT_ID; - if (getEnvVarValue("CI_PROJECT_ID")) - return t.skip("This test requires CI_PROJECT_ID be undefined. It is defined in .env file and so it is too annoying to work around."); - strictEqual(GitlabNugetRegistryInfo.projectId, undefined); - }); - - await it("returns string if CI_PROJECT_ID is defined", async () => { - if (!getEnvVarValue("CI_PROJECT_ID")) - process.env.CI_PROJECT_ID = "placeholder"; - strictEqual(GitlabNugetRegistryInfo.projectId, process.env.CI_PROJECT_ID) - }) - }) - -}); +import { describe, it, todo } from 'node:test' +import { ok, strictEqual } from 'node:assert/strict' +import { GitlabNugetRegistryInfo } from '@halospv3/hce.shared-config/dotnet/GitlabNugetRegistryInfo' +import { getEnv, getEnvVarValue } from '@halospv3/hce.shared-config/envUtils' + +await describe('GitlabNugetRegistryInfo', async (ctx0) => { + await it('has expected name', async () => { + strictEqual(GitlabNugetRegistryInfo.name, ctx0.name) + }) + + await describe('an instance of GitlabNugetRegistryInfo', async (ctx1) => { + ok(ctx1) + + if (!getEnvVarValue('CI_JOB_TOKEN')) + process.env.CI_JOB_TOKEN = 'placeholder' + if (!getEnvVarValue('CI_PROJECT_ID')) + process.env.CI_PROJECT_ID = 'placeholder' + const defaultWithPlaceholders = new GitlabNugetRegistryInfo() + + await it('defaults to project-level endpoint', async () => { + if (!getEnvVarValue('CI_PROJECT_ID')) + process.env.CI_PROJECT_ID = 'placeholder' + if (!getEnvVarValue('CI_JOB_TOKEN')) + process.env.CI_JOB_TOKEN = 'placeholder' + const expected = `${GitlabNugetRegistryInfo.CI_API_V4_URL}/projects/${GitlabNugetRegistryInfo.projectId}/packages/nuget/index.json` + strictEqual(new GitlabNugetRegistryInfo().url, expected) + }) + + await it('can be configured to use group-level endpoint', async () => { + if (!getEnvVarValue('CI_PROJECT_NAMESPACE_ID')) + process.env.CI_PROJECT_NAMESPACE_ID = 'placeholder' + if (!getEnvVarValue('CI_JOB_TOKEN')) + process.env.CI_JOB_TOKEN = 'placeholder' + const expected = `${GitlabNugetRegistryInfo.CI_API_V4_URL}/groups/${GitlabNugetRegistryInfo.ownerId}/-/packages/nuget/index.json` + strictEqual( + new GitlabNugetRegistryInfo(undefined, undefined, undefined, true).url, + expected, + ) + }) + + await it('throws if default values and no token available', async (t) => { + const { CI_JOB_TOKEN, GL_TOKEN, GITLAB_TOKEN } = getEnv() + delete process.env.CI_JOB_TOKEN + delete process.env.GL_TOKEN + delete process.env.GITLAB_TOKEN + if (getEnvVarValue('CI_JOB_TOKEN') || getEnvVarValue('GL_TOKEN') || getEnvVarValue('GITLAB_TOKEN')) + return t.skip('one or more tokens (CI_JOB_TOKEN, GL_TOKEN, GITLAB_TOKEN) is defined in .env file') + + let value: GitlabNugetRegistryInfo | Error + try { + value = new GitlabNugetRegistryInfo() + } + catch (err) { + value = err instanceof Error ? err : new Error(String(err)) + } + + ok(value instanceof Error) + + if (CI_JOB_TOKEN) + process.env.CI_JOB_TOKEN = CI_JOB_TOKEN + if (GL_TOKEN) + process.env.GL_TOKEN = GL_TOKEN + if (GITLAB_TOKEN) + process.env.GITLAB_TOKEN = GITLAB_TOKEN + }) + + await it('throws when custom values and no token available', async () => { + let value: GitlabNugetRegistryInfo | Error + try { + value = new GitlabNugetRegistryInfo(undefined, 'UNDEFINED_TOKEN', ['ANOTHER_UNDEFINED_TOKEN']) + } + catch (err) { + value = err instanceof Error ? err : new Error(String(err)) + } + ok(value instanceof Error) + }) + + await describe('canPushPackagesToUrl', async (ctx2) => { + await it('has expected name', async () => { + ok(ctx2.name in defaultWithPlaceholders) + }) + + await todo('can...uhhhh...Sorry. Brainrot.') + }) + + await describe('resolvedEnvVariable', async (ctx2) => { + await it('has expected name', async () => { + ok(ctx2.name in defaultWithPlaceholders) + }) + + await it('is a string', async () => { + strictEqual(typeof defaultWithPlaceholders.resolvedEnvVariable, 'string') + }) + }) + + /* await describe("toRegistryPair", async (ctx2) => { + await it("has expected name", async () => { + ok(ctx2.name in defaultWithPlaceholders); + }); + }); */ + + await describe('url', async (ctx2) => { + await it('has expected name', async () => { + ok(ctx2.name in defaultWithPlaceholders) + }) + + await it('is a string', async () => { + strictEqual(typeof defaultWithPlaceholders.url, 'string') + }) + }) + + /** + * value.toRegistryPair + * value.url + */ + }) + + await describe('CI_API_V4_URL', async (ctx1) => { + const expectedValue = 'https://gitlab.com/api/v4' + + await it('has expected name', async () => { + ok(ctx1.name in GitlabNugetRegistryInfo) + }) + + await it('has the correct default value if the environment variable is undefined', async () => { + delete process.env.CI_API_V4_URL + strictEqual(GitlabNugetRegistryInfo.CI_API_V4_URL, expectedValue) + }) + + // await it("has the correct value the value is provided by the environment variable") + }) + + await describe('groupUrl', async (ctx1) => { + await it('has expected name', async () => { + ok(ctx1.name in GitlabNugetRegistryInfo) + }) + + await it('returns the expected url when CI_PROJECT_NAMESPACE_ID is defined', async () => { + process.env.CI_PROJECT_NAMESPACE_ID = 'placeholder' + strictEqual( + GitlabNugetRegistryInfo.groupUrl, + `${GitlabNugetRegistryInfo.CI_API_V4_URL}/groups/${GitlabNugetRegistryInfo.ownerId}/-/packages/nuget/index.json`, + ) + }) + }) + + await describe('projectUrl', async (ctx1) => { + await it('has expected name', async () => { + ok(ctx1.name in GitlabNugetRegistryInfo) + }) + + await it('returns the expected url when CI_PROJECT_ID is defined', async () => { + if (!getEnvVarValue('CI_PROJECT_ID')) + process.env.CI_PROJECT_ID = 'placeholder' + strictEqual( + GitlabNugetRegistryInfo.projectUrl, + `${GitlabNugetRegistryInfo.CI_API_V4_URL}/projects/${GitlabNugetRegistryInfo.projectId}/packages/nuget/index.json`, + ) + }) + }) + + await describe('ownerId', async (ctx1) => { + await it('has expected name', async () => { + ok(ctx1.name in GitlabNugetRegistryInfo) + }) + + await it('returns undefined if CI_PROJECT_NAMESPACE_ID is undefined', async (t) => { + delete process.env.CI_PROJECT_NAMESPACE_ID + if (getEnvVarValue('CI_PROJECT_NAMESPACE_ID')) + return t.skip('This test requires CI_PROJECT_NAMESPACE_ID be undefined. It is defined in .env file and so it is too annoying to work around.') + strictEqual(GitlabNugetRegistryInfo.ownerId, undefined) + }) + + await it('returns string if CI_PROJECT_NAMESPACE_ID is defined', async () => { + if (!getEnvVarValue('CI_PROJECT_NAMESPACE_ID')) + process.env.CI_PROJECT_NAMESPACE_ID = 'placeholder' + strictEqual(GitlabNugetRegistryInfo.ownerId, process.env.CI_PROJECT_NAMESPACE_ID) + }) + }) + + await describe('projectId', async (ctx1) => { + await it('has expected name', async () => { + ok(ctx1.name in GitlabNugetRegistryInfo) + }) + + await it('returns undefined if CI_PROJECT_ID is undefined', async (t) => { + delete process.env.CI_PROJECT_ID + if (getEnvVarValue('CI_PROJECT_ID')) + return t.skip('This test requires CI_PROJECT_ID be undefined. It is defined in .env file and so it is too annoying to work around.') + strictEqual(GitlabNugetRegistryInfo.projectId, undefined) + }) + + await it('returns string if CI_PROJECT_ID is defined', async () => { + if (!getEnvVarValue('CI_PROJECT_ID')) + process.env.CI_PROJECT_ID = 'placeholder' + strictEqual(GitlabNugetRegistryInfo.projectId, process.env.CI_PROJECT_ID) + }) + }) +}) diff --git a/tests/envUtils.test.ts b/tests/envUtils.test.ts index cf466a07..2165a3cd 100644 --- a/tests/envUtils.test.ts +++ b/tests/envUtils.test.ts @@ -1,28 +1,28 @@ -import { getEnv, getEnvVarValue } from "@halospv3/hce.shared-config/envUtils"; -import { strictEqual } from "node:assert"; -import { env } from "node:process"; -import { describe, it } from "node:test"; +import { getEnv, getEnvVarValue } from '@halospv3/hce.shared-config/envUtils' +import { strictEqual } from 'node:assert' +import { env } from 'node:process' +import { describe, it } from 'node:test' await describe('envUtils', async () => { - await describe('getEnvVarValue', async () => { - await it('returns value of TEMP when told to do so', () => { - strictEqual(getEnvVarValue('TEMP'), env['TEMP']); - }); + await describe('getEnvVarValue', async () => { + await it('returns value of TEMP when told to do so', () => { + strictEqual(getEnvVarValue('TEMP'), env['TEMP']) + }) - await it("can load .env file and variable value in process.env is identical to value in .env file", { todo: true }); - }); + await it('can load .env file and variable value in process.env is identical to value in .env file', { todo: true }) + }) - await describe('getEnv', async () => { - await it("returns ProcessEnv object", () => { - function isProcessEnv(o: object): o is NodeJS.ProcessEnv { - return Object.keys(o).every(v => typeof v === 'string') && Object.values(o).every(v => v === undefined || typeof v === "string") - } - const o = getEnv() satisfies NodeJS.ProcessEnv; - strictEqual(isProcessEnv(o), true) - }); + await describe('getEnv', async () => { + await it('returns ProcessEnv object', () => { + function isProcessEnv(o: object): o is NodeJS.ProcessEnv { + return Object.keys(o).every(v => typeof v === 'string') && Object.values(o).every(v => v === undefined || typeof v === 'string') + } + const o = getEnv() satisfies NodeJS.ProcessEnv + strictEqual(isProcessEnv(o), true) + }) - await it("can pass dotenv options", { todo: true }); + await it('can pass dotenv options', { todo: true }) - await it("can override loaded variables", { todo: true }); - }) -}); \ No newline at end of file + await it('can override loaded variables', { todo: true }) + }) +}) diff --git a/tests/eslintConfig.test.ts b/tests/eslintConfig.test.ts index feda414e..1b67ce89 100644 --- a/tests/eslintConfig.test.ts +++ b/tests/eslintConfig.test.ts @@ -1,10 +1,10 @@ -import eslintConfig from "@halospv3/hce.shared-config/eslintConfig"; -import { ok } from "node:assert/strict"; -import { describe, it, todo } from "node:test"; +import eslintConfig from '@halospv3/hce.shared-config/eslintConfig' +import { ok } from 'node:assert/strict' +import { describe, it, todo } from 'node:test' -await describe("eslintConfig", async () => { - await it('exports an array', () => { - ok(Array.isArray(eslintConfig)) - }) - await todo('excludes certain paths from linting'); -}); \ No newline at end of file +await describe('eslintConfig', async () => { + await it('exports an array', () => { + ok(Array.isArray(eslintConfig)) + }) + await todo('excludes certain paths from linting') +}) diff --git a/tests/index.test.ts b/tests/index.test.ts index e3e2d094..5336e5a7 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -1,33 +1,33 @@ -import HceSharedConfig from '@halospv3/hce.shared-config'; +import HceSharedConfig from '@halospv3/hce.shared-config' import { - deepStrictEqual, - notStrictEqual, - strictEqual -} from 'node:assert/strict'; -import { describe, test } from 'node:test'; -import type { Options } from 'semantic-release'; + deepStrictEqual, + notStrictEqual, + strictEqual, +} from 'node:assert/strict' +import { describe, test } from 'node:test' +import type { Options } from 'semantic-release' -const options: Options = HceSharedConfig; +const options: Options = HceSharedConfig await describe('index', async () => { - await test('default export from index: semantic-release options', async () => { - await test('options is defined', () => { - notStrictEqual(options, undefined) - }); + await test('default export from index: semantic-release options', async () => { + await test('options is defined', () => { + notStrictEqual(options, undefined) + }) - await test('options.preset is conventionalcommits', () => { - strictEqual(options.preset, 'conventionalcommits') - }); + await test('options.preset is conventionalcommits', () => { + strictEqual(options.preset, 'conventionalcommits') + }) - await test('options.branches is mainline-main, prerelease-develop', () => { - deepStrictEqual(options.branches, [ - 'main', - { - name: 'develop', - channel: 'develop', - prerelease: true, - }, - ]); - }); - }); -}); \ No newline at end of file + await test('options.branches is mainline-main, prerelease-develop', () => { + deepStrictEqual(options.branches, [ + 'main', + { + name: 'develop', + channel: 'develop', + prerelease: true, + }, + ]) + }) + }) +}) diff --git a/tests/package.test.ts b/tests/package.test.ts index ff610c87..39599551 100644 --- a/tests/package.test.ts +++ b/tests/package.test.ts @@ -1,184 +1,186 @@ -import { describe, it } from "node:test"; +import { describe, it } from 'node:test' -import { deepStrictEqual, notStrictEqual } from "node:assert"; -import { spawnSync } from "node:child_process"; -import { createRequire } from "node:module"; -import { packemon } from "../package.json" with { type: "json" }; +import { deepStrictEqual, notStrictEqual } from 'node:assert' +import { spawnSync } from 'node:child_process' +import { createRequire } from 'node:module' +import { packemon } from '../package.json' with { type: 'json' } // import { isMainThread, Worker } from "node:worker_threads"; // #region PackemonTypes -type InputMap = Record; +type InputMap = Record interface PackemonPackageFeatures { - cjsTypesCompat?: boolean; - helpers?: 'bundled' | 'external' | 'inline' | 'runtime'; - swc?: boolean; + cjsTypesCompat?: boolean + helpers?: 'bundled' | 'external' | 'inline' | 'runtime' + swc?: boolean } -type CommonFormat = 'lib'; +type CommonFormat = 'lib' type BrowserFormat = - | CommonFormat - // ECMAScript modules with ".js" file extension - | 'esm' - // Universal Module Definition with ".js" file extension - | 'umd'; + | CommonFormat + // ECMAScript modules with ".js" file extension + | 'esm' + // Universal Module Definition with ".js" file extension + | 'umd' type NodeFormat = - | CommonFormat - /* CommonJS modules with ".cjs" file extension */ - | 'cjs' - /* ECMAScript modules with ".mjs" file extension*/ - | 'mjs'; -type Format = BrowserFormat | NodeFormat; -type Platform = 'browser' | 'electron' | 'native' | 'node'; + | CommonFormat + /* CommonJS modules with ".cjs" file extension */ + | 'cjs' + /* ECMAScript modules with ".mjs" file extension */ + | 'mjs' +type Format = BrowserFormat | NodeFormat +type Platform = 'browser' | 'electron' | 'native' | 'node' interface PackemonPackageConfig { - api?: 'private' | 'public'; - bundle?: boolean; - externals?: string[] | string; - features?: PackemonPackageFeatures; - format?: Format | Format[]; - inputs?: InputMap; - namespace?: string; - platform?: Platform | Platform[]; - support?: - // Latest version - | 'current' - // Next/future version - | 'experimental' - // Unsupported version - | 'legacy' - // Oldest version still supported - | 'stable'; + api?: 'private' | 'public' + bundle?: boolean + externals?: string[] | string + features?: PackemonPackageFeatures + format?: Format | Format[] + inputs?: InputMap + namespace?: string + platform?: Platform | Platform[] + support?: | + // Latest version + 'current' | + // Next/future version + 'experimental' | + // Unsupported version + 'legacy' | + // Oldest version still supported + 'stable' } // #endregion PackemonTypes -await describe("package.json", async () => { - // #region Types - interface Entry { - name: string; - source: string; - }; - interface Result { - action: 'require' | 'import', - entry: Entry, - validity: Error | boolean - }; - - // #endregion Types - - const require = createRequire(import.meta.url); - function tryCanAction(action: Result["action"], entry: Result["entry"]) { - const result = { action, entry, validity: false } as Result; - try { - const id = `@halospv3/hce.shared-config/${result.entry.name}`; - let verb; - if (result.action === "import") { - /* const resolvedId = */import.meta.resolve(id); - verb = "await import"; - } - else /** preResult.action === "require" */ { - /* const resolvedId = */require.resolve(id); - verb = "require" - } - - // const worker = new Worker(`const def = ${action}('${id})`); - const cp = spawnSync('node', ['-', `${verb}('${id}');.exit`], { encoding: "utf8" }); - if (cp.error ?? (cp.stderr.length > 0 && cp.stderr.replaceAll('\r\n', '') !== 'Debugger attached.Waiting for the debugger to disconnect...')) - throw cp.error ?? new Error(cp.stderr); - - result.validity = true; - return result; - } catch (error) { - if (error instanceof Error) - result.validity = error; - else if (typeof error === "string") - result.validity = new Error(error); - else - result.validity = new Error(String(error)); - return result; - } +await describe('package.json', async () => { + // #region Types + interface Entry { + name: string + source: string + }; + interface Result { + action: 'require' | 'import' + entry: Entry + validity: Error | boolean + }; + + // #endregion Types + + const require = createRequire(import.meta.url) + function tryCanAction(action: Result['action'], entry: Result['entry']) { + const result = { action, entry, validity: false } as Result + try { + const id = `@halospv3/hce.shared-config/${result.entry.name}` + let verb + if (result.action === 'import') { + /* const resolvedId = */import.meta.resolve(id) + verb = 'await import' + } + else /** preResult.action === "require" */ { + /* const resolvedId = */require.resolve(id) + verb = 'require' + } + + // const worker = new Worker(`const def = ${action}('${id})`); + const cp = spawnSync('node', ['-', `${verb}('${id}');.exit`], { encoding: 'utf8' }) + if (cp.error ?? (cp.stderr.length > 0 && cp.stderr.replaceAll('\r\n', '') !== 'Debugger attached.Waiting for the debugger to disconnect...')) + throw cp.error ?? new Error(cp.stderr) + + result.validity = true + return result } - - const nameof_expectedEsm = "expectedEsm"; - const expectedEsm: string[] = [ - "CaseInsensitiveMap", - "commitlintConfig", - "envUtils", - "eslintConfig", - "index", - "semanticReleaseConfig", - "semanticReleaseConfigDotnet", - "setupGitPluginSpec" - ]; - - // #region Arrays - const packemonArray = (Array.isArray(packemon) ? packemon : [packemon]) as PackemonPackageConfig[]; - const packemonMjs = packemonArray.find(v => v.format === "mjs" || true === v.format?.includes("mjs") || (v.format === undefined && v.platform === "node")); - const results: Result[] = []; - if (packemonMjs?.inputs) { - results.push( - ... - Object.entries(packemonMjs.inputs) - .map(entry => - tryCanAction('import', { name: entry[0], source: entry[1] }) - ) - ); + catch (error) { + if (error instanceof Error) + result.validity = error + else if (typeof error === 'string') + result.validity = new Error(error) + else + result.validity = new Error(String(error)) + return result } - - const importedEsm: string[] = []; - for (const result of results) { - // result.validity = await result.validity; - if (!(result.validity instanceof Error)) { - switch (result.action) { - case "import": - importedEsm.push(result.entry.name); - break; - default: - throw new Error(`unexpected validation action '${result.action}'.`) - } - } + } + + const nameof_expectedEsm = 'expectedEsm' + const expectedEsm: string[] = [ + 'CaseInsensitiveMap', + 'commitlintConfig', + 'envUtils', + 'eslintConfig', + 'index', + 'semanticReleaseConfig', + 'semanticReleaseConfigDotnet', + 'setupGitPluginSpec', + ] + + // #region Arrays + const packemonArray = (Array.isArray(packemon) ? packemon : [packemon]) as PackemonPackageConfig[] + const packemonMjs = packemonArray.find(v => v.format === 'mjs' || true === v.format?.includes('mjs') || (v.format === undefined && v.platform === 'node')) + const results: Result[] = [] + if (packemonMjs?.inputs) { + results.push( + ...Object.entries(packemonMjs.inputs) + .map(entry => + tryCanAction('import', { name: entry[0], source: entry[1] }), + ), + ) + } + + const importedEsm: string[] = [] + for (const result of results) { + // result.validity = await result.validity; + if (!(result.validity instanceof Error)) { + switch (result.action) { + case 'import': + importedEsm.push(result.entry.name) + break + default: + throw new Error(`unexpected validation action '${result.action}'.`) + } } - - // #endregion Arrays - - await it('...exposes no modules which fail to load', () => { - interface ErrorResult extends Omit { validity: Error } - const errored = results.filter((v) => v.validity instanceof Error) as ErrorResult[]; - errored.forEach(v => { console.debug(v) }) - deepStrictEqual( - errored, - [], - errored.map( - result => - `Unable to ${result.action} ${result.entry.source}. Reason:\n${result.validity.stack}` - ).join('\n') - ); - }); - - await it('...is expected to expose ESM, but was not configured to do so', () => { - if (expectedEsm.length > 0) { - notStrictEqual( - packemonMjs?.inputs, - undefined, - `packemon was not configured for ESM, but ESM modules were expected! Comment out entries in ${nameof_expectedEsm}.` - ); - } - }); - - const missingEsm = expectedEsm.filter(expected => !importedEsm.includes(expected)); - const unexpectedEsm = importedEsm.filter(imported => !expectedEsm.includes(imported)); - - await it('...exposes all expected ESM modules', () => { - deepStrictEqual( - missingEsm, - [], - `One or more input modules (${missingEsm.map(v => `"${v}"`).join(", ")}) were not imported! Is this a breaking change?`) - }); - await it('...exposes no unexpected ESM modules', () => { - deepStrictEqual( - unexpectedEsm, - [], - `One or more exposed modules were imported, but were not found in ${nameof_expectedEsm}. ` + - `Add the modules' names (${unexpectedEsm.map(v => `"${v}"`).join(", ")}) to './tests/package.test.ts#${nameof_expectedEsm}' to ensure they are not accidentally removed later!` - ); - }); -}); + } + + // #endregion Arrays + + await it('...exposes no modules which fail to load', () => { + interface ErrorResult extends Omit { validity: Error } + const errored = results.filter(v => v.validity instanceof Error) as ErrorResult[] + errored.forEach((v) => { + console.debug(v) + }) + deepStrictEqual( + errored, + [], + errored.map( + result => + `Unable to ${result.action} ${result.entry.source}. Reason:\n${result.validity.stack}`, + ).join('\n'), + ) + }) + + await it('...is expected to expose ESM, but was not configured to do so', () => { + if (expectedEsm.length > 0) { + notStrictEqual( + packemonMjs?.inputs, + undefined, + `packemon was not configured for ESM, but ESM modules were expected! Comment out entries in ${nameof_expectedEsm}.`, + ) + } + }) + + const missingEsm = expectedEsm.filter(expected => !importedEsm.includes(expected)) + const unexpectedEsm = importedEsm.filter(imported => !expectedEsm.includes(imported)) + + await it('...exposes all expected ESM modules', () => { + deepStrictEqual( + missingEsm, + [], + `One or more input modules (${missingEsm.map(v => `"${v}"`).join(', ')}) were not imported! Is this a breaking change?`) + }) + await it('...exposes no unexpected ESM modules', () => { + deepStrictEqual( + unexpectedEsm, + [], + `One or more exposed modules were imported, but were not found in ${nameof_expectedEsm}. ` + + `Add the modules' names (${unexpectedEsm.map(v => `"${v}"`).join(', ')}) to './tests/package.test.ts#${nameof_expectedEsm}' to ensure they are not accidentally removed later!`, + ) + }) +}) diff --git a/tests/setupGitPluginSpec.test.ts b/tests/setupGitPluginSpec.test.ts index b7e30979..154c924c 100644 --- a/tests/setupGitPluginSpec.test.ts +++ b/tests/setupGitPluginSpec.test.ts @@ -1,19 +1,19 @@ -import { setupGitPluginSpec } from '@halospv3/hce.shared-config/setupGitPluginSpec'; -import { deepStrictEqual, notDeepStrictEqual } from 'node:assert'; -import { describe, it } from 'node:test'; +import { setupGitPluginSpec } from '@halospv3/hce.shared-config/setupGitPluginSpec' +import { deepStrictEqual, notDeepStrictEqual } from 'node:assert' +import { describe, it } from 'node:test' await describe('setupGitPluginSpec', async () => { - await it('returns original array if it lacks Git PluginSpec', () => { - deepStrictEqual( - setupGitPluginSpec([]), - [] - ); - }); + await it('returns original array if it lacks Git PluginSpec', () => { + deepStrictEqual( + setupGitPluginSpec([]), + [], + ) + }) - await it('modifies provided array if it includes Git PluginSpec', () => { - notDeepStrictEqual( - setupGitPluginSpec(['@semantic-release/git']), - ['@semantic-release/git'] - ); - }); -}); + await it('modifies provided array if it includes Git PluginSpec', () => { + notDeepStrictEqual( + setupGitPluginSpec(['@semantic-release/git']), + ['@semantic-release/git'], + ) + }) +})