From c89aa60986f99011ad69f5e7d57cf1cb34593069 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Wed, 1 Feb 2023 16:47:01 +0100 Subject: [PATCH 1/2] docker: configDir Signed-off-by: CrazyMax --- __tests__/docker.test.ts | 26 +++++++++++++++++++++++++- src/docker.ts | 6 ++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/__tests__/docker.test.ts b/__tests__/docker.test.ts index 1ee1dfd..9735031 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,6 +25,28 @@ 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'); diff --git a/src/docker.ts b/src/docker.ts index dca238d..186c43e 100644 --- a/src/docker.ts +++ b/src/docker.ts @@ -14,9 +14,15 @@ * limitations under the License. */ +import os from 'os'; +import path from 'path'; import * as exec from '@actions/exec'; export class Docker { + static get configDir(): string { + return process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker'); + } + public static isAvailable(): boolean { let dockerAvailable = false; exec From 765b23685cbc626597d514dcbfa51b18593d3d5b Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Wed, 1 Feb 2023 16:49:46 +0100 Subject: [PATCH 2/2] docker: isAvailable use get Signed-off-by: CrazyMax --- __tests__/docker.test.ts | 2 +- src/buildx/buildx.ts | 2 +- src/docker.ts | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/__tests__/docker.test.ts b/__tests__/docker.test.ts index 9735031..a0381a4 100644 --- a/__tests__/docker.test.ts +++ b/__tests__/docker.test.ts @@ -50,7 +50,7 @@ describe('configDir', () => { 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 186c43e..2b86de7 100644 --- a/src/docker.ts +++ b/src/docker.ts @@ -23,7 +23,7 @@ export class Docker { return process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker'); } - public static isAvailable(): boolean { + static get isAvailable(): boolean { let dockerAvailable = false; exec .getExecOutput('docker', undefined, { @@ -45,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; } @@ -55,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; }