Skip to content

Commit

Permalink
chore: add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
agoldis committed Mar 8, 2024
1 parent a65202a commit 32682d1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 38 deletions.
14 changes: 14 additions & 0 deletions packages/cypress-cloud/lib/ciProvider/__tests__/ciProvider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getCommitDefaults } from "../merge";

describe("ciProvider", () => {
it("should resolve when provider is null and ghaEventDatais null", () => {
// eslint-disable-next-line turbo/no-undeclared-env-vars
process.env.TEAMCITY_VERSION = "1";
const result = getCommitDefaults({
ghaEventData: null,
});
expect(result).toEqual({
ghaEventData: null,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ SOFTWARE.
*/

import debugFn from "debug";

import _ from "lodash";
import { ValidationError } from "./errors";
import { GhaEventData } from "./git";

import { ValidationError } from "../errors";
import { GhaEventData } from "../git";

const debug = debugFn("currents:ci");

Expand Down Expand Up @@ -641,7 +641,7 @@ const _providerCommitParams = (): ProviderCommitParamsRes => {
};
};

type CiProviderData = {
export type CiProviderData = {
sha?: string;
branch?: string;
message?: string;
Expand All @@ -651,7 +651,7 @@ type CiProviderData = {
defaultBranch?: string;
remoteBranch?: string;
runAttempt?: string;
ghaEventData?: GhaEventData;
ghaEventData?: GhaEventData | null;
};

interface ProviderCommitParamsRes {
Expand Down Expand Up @@ -726,36 +726,3 @@ export function getCI(ciBuildId?: string) {
provider,
};
}

export function getCommitDefaults(existingInfo: CiProviderData) {
debug("git commit existing info");
debug(existingInfo);

const commitParamsObj = getCommitParams();

debug("commit info from provider environment variables: %O", commitParamsObj);

// based on the existingInfo properties
// merge in the commitParams if null or undefined
// defaulting back to null if all fails
// NOTE: only properties defined in "existingInfo" will be returned
const combined = _.transform(
existingInfo,
(
memo: { [memoKey: string]: string | GhaEventData | null },
value: string | GhaEventData | null,
key: string
) => {
return (memo[key] = _.defaultTo(
value ||
(commitParamsObj ? commitParamsObj[key as keyof CiProvider] : null),
null
));
}
);

debug("combined git and environment variables from provider");
debug(combined);

return combined;
}
1 change: 1 addition & 0 deletions packages/cypress-cloud/lib/ciProvider/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./merge";
40 changes: 40 additions & 0 deletions packages/cypress-cloud/lib/ciProvider/merge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import debugFn from "debug";
import _ from "lodash";

import { GhaEventData } from "../git";
import { CiProvider, CiProviderData, getCommitParams } from "./ciProvider";

const debug = debugFn("currents:ci");

export function getCommitDefaults(existingInfo: CiProviderData) {
debug("git commit existing info");
debug(existingInfo);

const commitParamsObj = getCommitParams();

debug("commit info from provider environment variables: %O", commitParamsObj);

// based on the existingInfo properties
// merge in the commitParams if null or undefined
// defaulting back to null if all fails
// NOTE: only properties defined in "existingInfo" will be returned
const combined = _.transform(
existingInfo,
(
memo: { [memoKey: string]: string | GhaEventData | null },
value: string | GhaEventData | null,
key: string
) => {
return (memo[key] = _.defaultTo(
value ||
(commitParamsObj ? commitParamsObj[key as keyof CiProvider] : null),
null
));
}
);

debug("combined git and environment variables from provider");
debug(combined);

return combined;
}

0 comments on commit 32682d1

Please sign in to comment.