Merge pull request #17 from crazy-max/docker-configdir

docker: configDir
This commit is contained in:
CrazyMax 2023-02-01 17:06:03 +01:00 committed by GitHub
commit d21d31d108
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 6 deletions

View File

@ -14,8 +14,10 @@
* limitations under the License.
*/
import {beforeEach, describe, expect, it, jest} from '@jest/globals';
import {afterEach, beforeEach, describe, expect, it, jest} from '@jest/globals';
import * as exec from '@actions/exec';
import path from 'path';
import osm = require('os');
import {Docker} from '../src/docker';
@ -23,10 +25,32 @@ beforeEach(() => {
jest.clearAllMocks();
});
describe('configDir', () => {
const originalEnv = process.env;
beforeEach(() => {
jest.resetModules();
process.env = {
...originalEnv,
DOCKER_CONFIG: '/var/docker/config'
};
});
afterEach(() => {
process.env = originalEnv;
});
it('returns default', async () => {
process.env.DOCKER_CONFIG = '';
jest.spyOn(osm, 'homedir').mockImplementation(() => path.join('/tmp', 'home'));
expect(Docker.configDir).toEqual(path.join('/tmp', 'home', '.docker'));
});
it('returns from env', async () => {
expect(Docker.configDir).toEqual('/var/docker/config');
});
});
describe('isAvailable', () => {
it('cli', () => {
const execSpy = jest.spyOn(exec, 'getExecOutput');
Docker.isAvailable();
Docker.isAvailable;
// eslint-disable-next-line jest/no-standalone-expect
expect(execSpy).toHaveBeenCalledWith(`docker`, undefined, {
silent: true,

View File

@ -39,7 +39,7 @@ export class Buildx {
this.context = opts.context;
this.inputs = new Inputs(this.context);
this.install = new Install({standalone: opts.standalone});
this.standalone = opts?.standalone ?? !Docker.isAvailable();
this.standalone = opts?.standalone ?? !Docker.isAvailable;
}
public getCommand(args: Array<string>) {

View File

@ -14,10 +14,16 @@
* limitations under the License.
*/
import os from 'os';
import path from 'path';
import * as exec from '@actions/exec';
export class Docker {
public static isAvailable(): boolean {
static get configDir(): string {
return process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
}
static get isAvailable(): boolean {
let dockerAvailable = false;
exec
.getExecOutput('docker', undefined, {
@ -39,7 +45,7 @@ export class Docker {
}
public static async printVersion(standalone?: boolean) {
const noDocker = standalone ?? !Docker.isAvailable();
const noDocker = standalone ?? !Docker.isAvailable;
if (noDocker) {
return;
}
@ -49,7 +55,7 @@ export class Docker {
}
public static async printInfo(standalone?: boolean) {
const noDocker = standalone ?? !Docker.isAvailable();
const noDocker = standalone ?? !Docker.isAvailable;
if (noDocker) {
return;
}