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:
30
__tests__/.helpers/os.ts
Normal file
30
__tests__/.helpers/os.ts
Normal 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);
|
||||||
|
};
|
||||||
@@ -14,12 +14,13 @@
|
|||||||
* limitations under the License.
|
* 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 fs from 'fs';
|
||||||
import os from 'os';
|
import os from 'os';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import * as rimraf from 'rimraf';
|
import * as rimraf from 'rimraf';
|
||||||
import osm = require('os');
|
|
||||||
|
import {mockArch, mockPlatform} from '../.helpers/os';
|
||||||
|
|
||||||
import {Install} from '../../src/buildx/install';
|
import {Install} from '../../src/buildx/install';
|
||||||
|
|
||||||
@@ -85,8 +86,8 @@ describe('download', () => {
|
|||||||
['linux', 's390x'],
|
['linux', 's390x'],
|
||||||
])(
|
])(
|
||||||
'acquires buildx for %s/%s', async (os, arch) => {
|
'acquires buildx for %s/%s', async (os, arch) => {
|
||||||
jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform);
|
mockPlatform(os as NodeJS.Platform);
|
||||||
jest.spyOn(osm, 'arch').mockImplementation(() => arch);
|
mockArch(arch);
|
||||||
const install = new Install();
|
const install = new Install();
|
||||||
const buildxBin = await install.download('latest');
|
const buildxBin = await install.download('latest');
|
||||||
expect(fs.existsSync(buildxBin)).toBe(true);
|
expect(fs.existsSync(buildxBin)).toBe(true);
|
||||||
|
|||||||
@@ -14,12 +14,13 @@
|
|||||||
* limitations under the License.
|
* 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 fs from 'fs';
|
||||||
import os from 'os';
|
import os from 'os';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import * as rimraf from 'rimraf';
|
import * as rimraf from 'rimraf';
|
||||||
import osm = require('os');
|
|
||||||
|
import {mockArch, mockPlatform} from '../.helpers/os';
|
||||||
|
|
||||||
import {Install} from '../../src/compose/install';
|
import {Install} from '../../src/compose/install';
|
||||||
|
|
||||||
@@ -85,8 +86,8 @@ describe('download', () => {
|
|||||||
['linux', 's390x'],
|
['linux', 's390x'],
|
||||||
])(
|
])(
|
||||||
'acquires compose for %s/%s', async (os, arch) => {
|
'acquires compose for %s/%s', async (os, arch) => {
|
||||||
jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform);
|
mockPlatform(os as NodeJS.Platform);
|
||||||
jest.spyOn(osm, 'arch').mockImplementation(() => arch);
|
mockArch(arch);
|
||||||
const install = new Install();
|
const install = new Install();
|
||||||
const composeBin = await install.download('latest');
|
const composeBin = await install.download('latest');
|
||||||
expect(fs.existsSync(composeBin)).toBe(true);
|
expect(fs.existsSync(composeBin)).toBe(true);
|
||||||
|
|||||||
@@ -14,12 +14,13 @@
|
|||||||
* limitations under the License.
|
* 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 fs from 'fs';
|
||||||
import os from 'os';
|
import os from 'os';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import * as rimraf from 'rimraf';
|
import * as rimraf from 'rimraf';
|
||||||
import osm = require('os');
|
|
||||||
|
import {mockArch, mockPlatform} from '../.helpers/os';
|
||||||
|
|
||||||
import {Install} from '../../src/cosign/install';
|
import {Install} from '../../src/cosign/install';
|
||||||
|
|
||||||
@@ -80,8 +81,8 @@ describe('download', () => {
|
|||||||
['linux', 'arm64']
|
['linux', 'arm64']
|
||||||
])(
|
])(
|
||||||
'acquires undock for %s/%s', async (os, arch) => {
|
'acquires undock for %s/%s', async (os, arch) => {
|
||||||
jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform);
|
mockPlatform(os as NodeJS.Platform);
|
||||||
jest.spyOn(osm, 'arch').mockImplementation(() => arch);
|
mockArch(arch);
|
||||||
const install = new Install();
|
const install = new Install();
|
||||||
const cosignBin = await install.download({
|
const cosignBin = await install.download({
|
||||||
version: 'latest'
|
version: 'latest'
|
||||||
|
|||||||
@@ -19,9 +19,10 @@ import fs from 'fs';
|
|||||||
import os from 'os';
|
import os from 'os';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import * as io from '@actions/io';
|
import * as io from '@actions/io';
|
||||||
import osm = require('os');
|
|
||||||
import * as rimraf from 'rimraf';
|
import * as rimraf from 'rimraf';
|
||||||
|
|
||||||
|
import {mockHomedir} from '../.helpers/os';
|
||||||
|
|
||||||
import {Docker} from '../../src/docker/docker';
|
import {Docker} from '../../src/docker/docker';
|
||||||
|
|
||||||
import {ConfigFile} from '../../src/types/docker/docker';
|
import {ConfigFile} from '../../src/types/docker/docker';
|
||||||
@@ -47,7 +48,7 @@ describe('configDir', () => {
|
|||||||
});
|
});
|
||||||
it('returns default', async () => {
|
it('returns default', async () => {
|
||||||
process.env.DOCKER_CONFIG = '';
|
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'));
|
expect(Docker.configDir).toEqual(path.join('/tmp', 'home', '.docker'));
|
||||||
});
|
});
|
||||||
it('returns from env', async () => {
|
it('returns from env', async () => {
|
||||||
|
|||||||
@@ -19,7 +19,8 @@ import fs from 'fs';
|
|||||||
import os from 'os';
|
import os from 'os';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import * as rimraf from 'rimraf';
|
import * as rimraf from 'rimraf';
|
||||||
import osm = require('os');
|
|
||||||
|
import {mockArch, mockPlatform} from '../.helpers/os';
|
||||||
|
|
||||||
import {Install, InstallSourceArchive, InstallSourceImage} from '../../src/docker/install';
|
import {Install, InstallSourceArchive, InstallSourceImage} from '../../src/docker/install';
|
||||||
|
|
||||||
@@ -60,8 +61,8 @@ describe('download', () => {
|
|||||||
[image('27.3.1'), 'win32'],
|
[image('27.3.1'), 'win32'],
|
||||||
])(
|
])(
|
||||||
'acquires %p of docker (%s)', async (source, platformOS) => {
|
'acquires %p of docker (%s)', async (source, platformOS) => {
|
||||||
jest.spyOn(osm, 'platform').mockImplementation(() => platformOS as NodeJS.Platform);
|
mockPlatform(platformOS as NodeJS.Platform);
|
||||||
jest.spyOn(osm, 'arch').mockImplementation(() => 'x64');
|
mockArch('x64');
|
||||||
const install = new Install({
|
const install = new Install({
|
||||||
source: source,
|
source: source,
|
||||||
runDir: tmpDir
|
runDir: tmpDir
|
||||||
|
|||||||
@@ -14,12 +14,13 @@
|
|||||||
* limitations under the License.
|
* 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 fs from 'fs';
|
||||||
import os from 'os';
|
import os from 'os';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import * as rimraf from 'rimraf';
|
import * as rimraf from 'rimraf';
|
||||||
import osm = require('os');
|
|
||||||
|
import {mockArch, mockPlatform} from '../.helpers/os';
|
||||||
|
|
||||||
import {OCI} from '../../src/oci/oci';
|
import {OCI} from '../../src/oci/oci';
|
||||||
|
|
||||||
@@ -44,8 +45,8 @@ describe('defaultPlatform', () => {
|
|||||||
['linux', 'ppc64', {architecture: 'ppc64le', os: 'linux'}],
|
['linux', 'ppc64', {architecture: 'ppc64le', os: 'linux'}],
|
||||||
['linux', 's390x', {architecture: 's390x', os: 'linux'}]
|
['linux', 's390x', {architecture: 's390x', os: 'linux'}]
|
||||||
])('default platform for %s/%s', async (os: string, arch: string, expected: Platform) => {
|
])('default platform for %s/%s', async (os: string, arch: string, expected: Platform) => {
|
||||||
jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform);
|
mockPlatform(os as NodeJS.Platform);
|
||||||
jest.spyOn(osm, 'arch').mockImplementation(() => arch);
|
mockArch(arch);
|
||||||
const res = OCI.defaultPlatform();
|
const res = OCI.defaultPlatform();
|
||||||
expect(res).toEqual(expected);
|
expect(res).toEqual(expected);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -14,12 +14,13 @@
|
|||||||
* limitations under the License.
|
* 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 fs from 'fs';
|
||||||
import os from 'os';
|
import os from 'os';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import * as rimraf from 'rimraf';
|
import * as rimraf from 'rimraf';
|
||||||
import osm = require('os');
|
|
||||||
|
import {mockArch, mockPlatform} from '../.helpers/os';
|
||||||
|
|
||||||
import {Install} from '../../src/regclient/install';
|
import {Install} from '../../src/regclient/install';
|
||||||
|
|
||||||
@@ -75,8 +76,8 @@ describe('download', () => {
|
|||||||
['linux', 's390x'],
|
['linux', 's390x'],
|
||||||
])(
|
])(
|
||||||
'acquires regclient for %s/%s', async (os, arch) => {
|
'acquires regclient for %s/%s', async (os, arch) => {
|
||||||
jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform);
|
mockPlatform(os as NodeJS.Platform);
|
||||||
jest.spyOn(osm, 'arch').mockImplementation(() => arch);
|
mockArch(arch);
|
||||||
const install = new Install();
|
const install = new Install();
|
||||||
const regclientBin = await install.download('latest');
|
const regclientBin = await install.download('latest');
|
||||||
expect(fs.existsSync(regclientBin)).toBe(true);
|
expect(fs.existsSync(regclientBin)).toBe(true);
|
||||||
|
|||||||
@@ -14,12 +14,13 @@
|
|||||||
* limitations under the License.
|
* 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 fs from 'fs';
|
||||||
import os from 'os';
|
import os from 'os';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import * as rimraf from 'rimraf';
|
import * as rimraf from 'rimraf';
|
||||||
import osm = require('os');
|
|
||||||
|
import {mockArch, mockPlatform} from '../.helpers/os';
|
||||||
|
|
||||||
import {Install} from '../../src/undock/install';
|
import {Install} from '../../src/undock/install';
|
||||||
|
|
||||||
@@ -80,8 +81,8 @@ describe('download', () => {
|
|||||||
['linux', 's390x'],
|
['linux', 's390x'],
|
||||||
])(
|
])(
|
||||||
'acquires undock for %s/%s', async (os, arch) => {
|
'acquires undock for %s/%s', async (os, arch) => {
|
||||||
jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform);
|
mockPlatform(os as NodeJS.Platform);
|
||||||
jest.spyOn(osm, 'arch').mockImplementation(() => arch);
|
mockArch(arch);
|
||||||
const install = new Install();
|
const install = new Install();
|
||||||
const undockBin = await install.download('latest');
|
const undockBin = await install.download('latest');
|
||||||
expect(fs.existsSync(undockBin)).toBe(true);
|
expect(fs.existsSync(undockBin)).toBe(true);
|
||||||
|
|||||||
@@ -41,7 +41,8 @@ module.exports = {
|
|||||||
'^.+\\.ts$': [
|
'^.+\\.ts$': [
|
||||||
'ts-jest',
|
'ts-jest',
|
||||||
{
|
{
|
||||||
useESM: true
|
useESM: true,
|
||||||
|
tsconfig: '<rootDir>/tsconfig.test.json'
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ module.exports = {
|
|||||||
'^.+\\.ts$': [
|
'^.+\\.ts$': [
|
||||||
'ts-jest',
|
'ts-jest',
|
||||||
{
|
{
|
||||||
useESM: true
|
useESM: true,
|
||||||
|
tsconfig: '<rootDir>/tsconfig.test.json'
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
15
tsconfig.test.json
Normal file
15
tsconfig.test.json
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"extends": "./tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"rootDir": ".",
|
||||||
|
"noEmit": true
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"src/**/*.ts",
|
||||||
|
"__tests__/**/*.ts"
|
||||||
|
],
|
||||||
|
"exclude": [
|
||||||
|
"lib",
|
||||||
|
"node_modules"
|
||||||
|
]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user