add ESM-safe os test helpers and tsconfig for tests

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2026-01-28 11:52:41 +01:00
parent 8a49a6ea9c
commit 3f7be6d97d
12 changed files with 86 additions and 31 deletions

30
__tests__/.helpers/os.ts Normal file
View File

@@ -0,0 +1,30 @@
/**
* Copyright 2025 actions-toolkit authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {jest} from '@jest/globals';
import os from 'os';
export const mockPlatform = (platform: NodeJS.Platform) => {
return jest.spyOn(os, 'platform').mockImplementation(() => platform);
};
export const mockArch = (arch: string) => {
return jest.spyOn(os, 'arch').mockImplementation(() => arch);
};
export const mockHomedir = (dir: string) => {
return jest.spyOn(os, 'homedir').mockImplementation(() => dir);
};

View File

@@ -14,12 +14,13 @@
* limitations under the License.
*/
import {describe, expect, it, jest, test, afterEach} from '@jest/globals';
import {describe, expect, it, test, afterEach} from '@jest/globals';
import fs from 'fs';
import os from 'os';
import path from 'path';
import * as rimraf from 'rimraf';
import osm = require('os');
import {mockArch, mockPlatform} from '../.helpers/os';
import {Install} from '../../src/buildx/install';
@@ -85,8 +86,8 @@ describe('download', () => {
['linux', 's390x'],
])(
'acquires buildx for %s/%s', async (os, arch) => {
jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform);
jest.spyOn(osm, 'arch').mockImplementation(() => arch);
mockPlatform(os as NodeJS.Platform);
mockArch(arch);
const install = new Install();
const buildxBin = await install.download('latest');
expect(fs.existsSync(buildxBin)).toBe(true);

View File

@@ -14,12 +14,13 @@
* limitations under the License.
*/
import {describe, expect, it, jest, test, afterEach} from '@jest/globals';
import {describe, expect, it, test, afterEach} from '@jest/globals';
import fs from 'fs';
import os from 'os';
import path from 'path';
import * as rimraf from 'rimraf';
import osm = require('os');
import {mockArch, mockPlatform} from '../.helpers/os';
import {Install} from '../../src/compose/install';
@@ -85,8 +86,8 @@ describe('download', () => {
['linux', 's390x'],
])(
'acquires compose for %s/%s', async (os, arch) => {
jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform);
jest.spyOn(osm, 'arch').mockImplementation(() => arch);
mockPlatform(os as NodeJS.Platform);
mockArch(arch);
const install = new Install();
const composeBin = await install.download('latest');
expect(fs.existsSync(composeBin)).toBe(true);

View File

@@ -14,12 +14,13 @@
* limitations under the License.
*/
import {describe, expect, it, jest, test, afterEach} from '@jest/globals';
import {describe, expect, it, test, afterEach} from '@jest/globals';
import fs from 'fs';
import os from 'os';
import path from 'path';
import * as rimraf from 'rimraf';
import osm = require('os');
import {mockArch, mockPlatform} from '../.helpers/os';
import {Install} from '../../src/cosign/install';
@@ -80,8 +81,8 @@ describe('download', () => {
['linux', 'arm64']
])(
'acquires undock for %s/%s', async (os, arch) => {
jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform);
jest.spyOn(osm, 'arch').mockImplementation(() => arch);
mockPlatform(os as NodeJS.Platform);
mockArch(arch);
const install = new Install();
const cosignBin = await install.download({
version: 'latest'

View File

@@ -19,9 +19,10 @@ import fs from 'fs';
import os from 'os';
import path from 'path';
import * as io from '@actions/io';
import osm = require('os');
import * as rimraf from 'rimraf';
import {mockHomedir} from '../.helpers/os';
import {Docker} from '../../src/docker/docker';
import {ConfigFile} from '../../src/types/docker/docker';
@@ -47,7 +48,7 @@ describe('configDir', () => {
});
it('returns default', async () => {
process.env.DOCKER_CONFIG = '';
jest.spyOn(osm, 'homedir').mockImplementation(() => path.join('/tmp', 'home'));
mockHomedir(path.join('/tmp', 'home'));
expect(Docker.configDir).toEqual(path.join('/tmp', 'home', '.docker'));
});
it('returns from env', async () => {

View File

@@ -19,7 +19,8 @@ import fs from 'fs';
import os from 'os';
import path from 'path';
import * as rimraf from 'rimraf';
import osm = require('os');
import {mockArch, mockPlatform} from '../.helpers/os';
import {Install, InstallSourceArchive, InstallSourceImage} from '../../src/docker/install';
@@ -60,8 +61,8 @@ describe('download', () => {
[image('27.3.1'), 'win32'],
])(
'acquires %p of docker (%s)', async (source, platformOS) => {
jest.spyOn(osm, 'platform').mockImplementation(() => platformOS as NodeJS.Platform);
jest.spyOn(osm, 'arch').mockImplementation(() => 'x64');
mockPlatform(platformOS as NodeJS.Platform);
mockArch('x64');
const install = new Install({
source: source,
runDir: tmpDir

View File

@@ -14,12 +14,13 @@
* limitations under the License.
*/
import {afterEach, describe, expect, jest, test} from '@jest/globals';
import {afterEach, describe, expect, test} from '@jest/globals';
import fs from 'fs';
import os from 'os';
import path from 'path';
import * as rimraf from 'rimraf';
import osm = require('os');
import {mockArch, mockPlatform} from '../.helpers/os';
import {OCI} from '../../src/oci/oci';
@@ -44,8 +45,8 @@ describe('defaultPlatform', () => {
['linux', 'ppc64', {architecture: 'ppc64le', os: 'linux'}],
['linux', 's390x', {architecture: 's390x', os: 'linux'}]
])('default platform for %s/%s', async (os: string, arch: string, expected: Platform) => {
jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform);
jest.spyOn(osm, 'arch').mockImplementation(() => arch);
mockPlatform(os as NodeJS.Platform);
mockArch(arch);
const res = OCI.defaultPlatform();
expect(res).toEqual(expected);
});

View File

@@ -14,12 +14,13 @@
* limitations under the License.
*/
import {describe, expect, it, jest, test, afterEach} from '@jest/globals';
import {describe, expect, it, test, afterEach} from '@jest/globals';
import fs from 'fs';
import os from 'os';
import path from 'path';
import * as rimraf from 'rimraf';
import osm = require('os');
import {mockArch, mockPlatform} from '../.helpers/os';
import {Install} from '../../src/regclient/install';
@@ -75,8 +76,8 @@ describe('download', () => {
['linux', 's390x'],
])(
'acquires regclient for %s/%s', async (os, arch) => {
jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform);
jest.spyOn(osm, 'arch').mockImplementation(() => arch);
mockPlatform(os as NodeJS.Platform);
mockArch(arch);
const install = new Install();
const regclientBin = await install.download('latest');
expect(fs.existsSync(regclientBin)).toBe(true);

View File

@@ -14,12 +14,13 @@
* limitations under the License.
*/
import {describe, expect, it, jest, test, afterEach} from '@jest/globals';
import {describe, expect, it, test, afterEach} from '@jest/globals';
import fs from 'fs';
import os from 'os';
import path from 'path';
import * as rimraf from 'rimraf';
import osm = require('os');
import {mockArch, mockPlatform} from '../.helpers/os';
import {Install} from '../../src/undock/install';
@@ -80,8 +81,8 @@ describe('download', () => {
['linux', 's390x'],
])(
'acquires undock for %s/%s', async (os, arch) => {
jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform);
jest.spyOn(osm, 'arch').mockImplementation(() => arch);
mockPlatform(os as NodeJS.Platform);
mockArch(arch);
const install = new Install();
const undockBin = await install.download('latest');
expect(fs.existsSync(undockBin)).toBe(true);

View File

@@ -41,7 +41,8 @@ module.exports = {
'^.+\\.ts$': [
'ts-jest',
{
useESM: true
useESM: true,
tsconfig: '<rootDir>/tsconfig.test.json'
}
]
},

View File

@@ -24,7 +24,8 @@ module.exports = {
'^.+\\.ts$': [
'ts-jest',
{
useESM: true
useESM: true,
tsconfig: '<rootDir>/tsconfig.test.json'
}
]
},

15
tsconfig.test.json Normal file
View File

@@ -0,0 +1,15 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"rootDir": ".",
"noEmit": true
},
"include": [
"src/**/*.ts",
"__tests__/**/*.ts"
],
"exclude": [
"lib",
"node_modules"
]
}