2023-01-31 03:34:51 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright 2023 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2026-02-16 11:51:07 +01:00
|
|
|
import {describe, expect, vi, test, afterEach} from 'vitest';
|
2024-07-31 11:01:57 +02:00
|
|
|
import fs from 'fs';
|
|
|
|
|
import os from 'os';
|
|
|
|
|
import path from 'path';
|
2023-02-01 12:46:43 +01:00
|
|
|
import * as rimraf from 'rimraf';
|
2023-01-17 11:53:57 +01:00
|
|
|
|
2026-02-16 12:02:30 +01:00
|
|
|
import {BuildKit} from '../../src/buildkit/buildkit.js';
|
|
|
|
|
import {Context} from '../../src/context.js';
|
2023-01-31 21:13:39 +01:00
|
|
|
|
2024-07-31 11:04:17 +02:00
|
|
|
const fixturesDir = path.join(__dirname, '..', '.fixtures');
|
2024-07-31 11:01:57 +02:00
|
|
|
const tmpDir = fs.mkdtempSync(path.join(process.env.TEMP || os.tmpdir(), 'buildkit-config-'));
|
2026-02-16 11:51:07 +01:00
|
|
|
const tmpName = path.join(tmpDir, '.tmpname-vi');
|
2023-01-23 10:07:14 +01:00
|
|
|
|
2026-02-16 11:51:07 +01:00
|
|
|
vi.spyOn(Context, 'tmpDir').mockImplementation((): string => {
|
2024-07-31 11:01:57 +02:00
|
|
|
fs.mkdirSync(tmpDir, {recursive: true});
|
2023-01-23 10:07:14 +01:00
|
|
|
return tmpDir;
|
|
|
|
|
});
|
2023-02-20 07:19:57 +01:00
|
|
|
|
2026-02-16 11:51:07 +01:00
|
|
|
vi.spyOn(Context, 'tmpName').mockImplementation((): string => {
|
2023-01-23 10:07:14 +01:00
|
|
|
return tmpName;
|
2023-01-17 11:53:57 +01:00
|
|
|
});
|
|
|
|
|
|
2023-01-23 10:07:14 +01:00
|
|
|
afterEach(() => {
|
|
|
|
|
rimraf.sync(tmpDir);
|
2023-01-17 11:53:57 +01:00
|
|
|
});
|
|
|
|
|
|
2023-02-03 14:45:32 +01:00
|
|
|
describe('resolve', () => {
|
2023-01-17 11:53:57 +01:00
|
|
|
test.each([
|
2023-01-23 10:07:14 +01:00
|
|
|
['debug = true', false, 'debug = true', null],
|
|
|
|
|
[`notfound.toml`, true, '', new Error('config file notfound.toml not found')],
|
2023-01-17 11:53:57 +01:00
|
|
|
[
|
2023-02-19 00:59:04 +01:00
|
|
|
`${path.join(fixturesDir, 'buildkitd.toml')}`,
|
2023-01-17 11:53:57 +01:00
|
|
|
true,
|
|
|
|
|
`debug = true
|
|
|
|
|
[registry."docker.io"]
|
|
|
|
|
mirrors = ["mirror.gcr.io"]
|
|
|
|
|
`,
|
2023-01-23 10:07:14 +01:00
|
|
|
null
|
2023-01-17 11:53:57 +01:00
|
|
|
]
|
2026-03-01 11:51:12 +01:00
|
|
|
])('given %o config', async (val: string, file: boolean, exValue: string, error: Error | null) => {
|
2023-01-17 11:53:57 +01:00
|
|
|
try {
|
2023-02-20 07:19:57 +01:00
|
|
|
const buildkit = new BuildKit();
|
2023-01-17 11:53:57 +01:00
|
|
|
let config: string;
|
|
|
|
|
if (file) {
|
2023-02-03 14:45:32 +01:00
|
|
|
config = buildkit.config.resolveFromFile(val);
|
2023-01-17 11:53:57 +01:00
|
|
|
} else {
|
2023-02-03 14:45:32 +01:00
|
|
|
config = buildkit.config.resolveFromString(val);
|
2023-01-17 11:53:57 +01:00
|
|
|
}
|
2023-01-23 10:07:14 +01:00
|
|
|
expect(config).toEqual(tmpName);
|
|
|
|
|
const configValue = fs.readFileSync(tmpName, 'utf-8');
|
2023-01-17 11:53:57 +01:00
|
|
|
expect(configValue).toEqual(exValue);
|
2023-01-23 10:07:14 +01:00
|
|
|
} catch (e) {
|
2026-02-16 11:51:07 +01:00
|
|
|
// eslint-disable-next-line vitest/no-conditional-expect
|
2023-01-23 10:07:14 +01:00
|
|
|
expect(e.message).toEqual(error?.message);
|
2023-01-17 11:53:57 +01:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|