diff --git a/__tests__/docker.test.ts b/__tests__/docker.test.ts index 1ee1dfd..a0381a4 100644 --- a/__tests__/docker.test.ts +++ b/__tests__/docker.test.ts @@ -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, diff --git a/src/buildx/buildx.ts b/src/buildx/buildx.ts index ed3fce2..a46cdf4 100644 --- a/src/buildx/buildx.ts +++ b/src/buildx/buildx.ts @@ -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) { diff --git a/src/docker.ts b/src/docker.ts index dca238d..2b86de7 100644 --- a/src/docker.ts +++ b/src/docker.ts @@ -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; }