diff --git a/src/dev/build/lib/get_build_number.test.ts b/src/dev/build/lib/get_build_number.test.ts new file mode 100644 index 000000000000..438235b89362 --- /dev/null +++ b/src/dev/build/lib/get_build_number.test.ts @@ -0,0 +1,41 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +import { getBuildNumber } from './get_build_number'; + +const BUILD_NUMBER_ENV_KEY = 'BUILD_NUMBER'; +const itif = process.env.CI === '1' ? it.skip : it; + +describe('getBuildNumber', () => { + let previousBuildNumber: string; + + beforeEach(() => { + if (BUILD_NUMBER_ENV_KEY in process.env) { + previousBuildNumber = process.env[BUILD_NUMBER_ENV_KEY] as string; + delete process.env[BUILD_NUMBER_ENV_KEY]; + } + }); + + afterEach(() => { + if (BUILD_NUMBER_ENV_KEY in process.env) { + process.env[BUILD_NUMBER_ENV_KEY] = previousBuildNumber; + } + }); + + it('returns env BUILD_NUMBER count', async () => { + process.env.BUILD_NUMBER = '123'; + const buildNumber = await getBuildNumber(); + expect(buildNumber).toBe(123); + }); + + // If test is ran on the CI, it only gets 1 commit + itif('returns git commit count', async () => { + const buildNumber = await getBuildNumber(); + expect(buildNumber).toBeGreaterThan(1000); + }); +}); diff --git a/src/dev/build/lib/get_build_number.ts b/src/dev/build/lib/get_build_number.ts index 333adcf3bd00..99b846b3cc5f 100644 --- a/src/dev/build/lib/get_build_number.ts +++ b/src/dev/build/lib/get_build_number.ts @@ -29,6 +29,10 @@ import os from 'os'; import execa from 'execa'; export async function getBuildNumber() { + if ('BUILD_NUMBER' in process.env) { + return parseFloat(process.env.BUILD_NUMBER as string); + } + if (/^win/.test(os.platform())) { // Windows does not have the wc process and `find /C /V ""` does not consistently work const log = await execa('git', ['log', '--format="%h"']);