diff --git a/__tests__/PowerShell/AzPSLogin.test.ts b/__tests__/PowerShell/AzPSLogin.test.ts index 666706ad6..3c4cbaeff 100644 --- a/__tests__/PowerShell/AzPSLogin.test.ts +++ b/__tests__/PowerShell/AzPSLogin.test.ts @@ -1,9 +1,8 @@ import * as os from 'os'; import { AzPSLogin } from '../../src/PowerShell/AzPSLogin'; -import AzPSConfig from '../../src/PowerShell/AzPSConfig'; import { LoginConfig } from '../../src/common/LoginConfig'; -import AzPSConstants from '../../src/PowerShell/AzPSConstants'; +import { AzPSConstants, AzPSUtils } from '../../src/PowerShell/AzPSUtils'; let azpsLogin: AzPSLogin; jest.setTimeout(30000); @@ -37,7 +36,7 @@ describe('Testing login', () => { describe('Testing set module path', () => { test('setDefaultPSModulePath should work', () => { - AzPSConfig.setPSModulePathForGitHubRunner(); + AzPSUtils.setPSModulePathForGitHubRunner(); const runner: string = process.env.RUNNER_OS || os.type(); if(runner.toLowerCase() === "linux"){ expect(process.env.PSModulePath).toContain(AzPSConstants.DEFAULT_AZ_PATH_ON_LINUX); @@ -64,7 +63,7 @@ describe('Testing runPSScript', () => { } return ConvertTo-Json $output`; - let psVersion: string = await AzPSConfig.runPSScript(script); + let psVersion: string = await AzPSUtils.runPSScript(script); expect(psVersion === null).toBeFalsy(); }); @@ -83,7 +82,7 @@ describe('Testing runPSScript', () => { return ConvertTo-Json $output`; try{ - await AzPSConfig.runPSScript(script); + await AzPSUtils.runPSScript(script); throw new Error("The last step should fail."); }catch(error){ expect(error.message.includes("Azure PowerShell login failed with error: You cannot call a method on a null-valued expression.")).toBeTruthy(); diff --git a/src/PowerShell/AzPSConstants.ts b/src/PowerShell/AzPSConstants.ts deleted file mode 100644 index d0b569e1b..000000000 --- a/src/PowerShell/AzPSConstants.ts +++ /dev/null @@ -1,7 +0,0 @@ -export default class AzPSConstants { - static readonly DEFAULT_AZ_PATH_ON_LINUX: string = '/usr/share'; - static readonly DEFAULT_AZ_PATH_ON_WINDOWS: string = 'C:\\Modules'; - static readonly AzAccounts: string = "Az.Accounts"; - static readonly PowerShell_CmdName = "pwsh"; -} - diff --git a/src/PowerShell/AzPSLogin.ts b/src/PowerShell/AzPSLogin.ts index 71e78aa40..6e0c7bd6a 100644 --- a/src/PowerShell/AzPSLogin.ts +++ b/src/PowerShell/AzPSLogin.ts @@ -1,7 +1,7 @@ import * as core from '@actions/core'; import AzPSScriptBuilder from './AzPSScriptBuilder'; -import AzPSConfig from './AzPSConfig'; +import { AzPSUtils } from './AzPSUtils'; import { LoginConfig } from '../common/LoginConfig'; export class AzPSLogin { @@ -13,12 +13,12 @@ export class AzPSLogin { async login() { core.info(`Running Azure PowerShell Login.`); - AzPSConfig.setPSModulePathForGitHubRunner(); - await AzPSConfig.importLatestAzAccounts(); + AzPSUtils.setPSModulePathForGitHubRunner(); + await AzPSUtils.importLatestAzAccounts(); const [loginMethod, loginScript] = await AzPSScriptBuilder.getAzPSLoginScript(this.loginConfig); core.info(`Attempting Azure PowerShell login by using ${loginMethod}...`); core.debug(`Azure PowerShell Login Script: ${loginScript}`); - await AzPSConfig.runPSScript(loginScript); + await AzPSUtils.runPSScript(loginScript); console.log(`Running Azure PowerShell Login successfully.`); } } diff --git a/src/PowerShell/AzPSScriptBuilder.ts b/src/PowerShell/AzPSScriptBuilder.ts index 619312eaa..28e8a95fd 100644 --- a/src/PowerShell/AzPSScriptBuilder.ts +++ b/src/PowerShell/AzPSScriptBuilder.ts @@ -1,4 +1,3 @@ -import AzPSConstants from "./AzPSConstants"; import { LoginConfig } from '../common/LoginConfig'; export default class AzPSScriptBuilder { diff --git a/src/PowerShell/AzPSConfig.ts b/src/PowerShell/AzPSUtils.ts similarity index 82% rename from src/PowerShell/AzPSConfig.ts rename to src/PowerShell/AzPSUtils.ts index a4ac371d1..4b287cfd7 100644 --- a/src/PowerShell/AzPSConfig.ts +++ b/src/PowerShell/AzPSUtils.ts @@ -3,7 +3,6 @@ import * as os from 'os'; import * as path from 'path'; import * as exec from '@actions/exec'; import * as io from '@actions/io'; -import AzPSConstants from './AzPSConstants'; import AzPSScriptBuilder from './AzPSScriptBuilder'; interface PSResultType { @@ -12,16 +11,23 @@ interface PSResultType { Error: string; } -export default class AzPSConfig { +export class AzPSConstants { + static readonly DEFAULT_AZ_PATH_ON_LINUX: string = '/usr/share'; + static readonly DEFAULT_AZ_PATH_ON_WINDOWS: string = 'C:\\Modules'; + static readonly AzAccounts: string = "Az.Accounts"; + static readonly PowerShell_CmdName = "pwsh"; +} + +export class AzPSUtils { static async setPSModulePathForGitHubRunner() { const runner: string = process.env.RUNNER_OS || os.type(); switch (runner.toLowerCase()) { case "linux": - AzPSConfig.pushPSModulePath(AzPSConstants.DEFAULT_AZ_PATH_ON_LINUX); + AzPSUtils.pushPSModulePath(AzPSConstants.DEFAULT_AZ_PATH_ON_LINUX); break; case "windows": case "windows_nt": - AzPSConfig.pushPSModulePath(AzPSConstants.DEFAULT_AZ_PATH_ON_WINDOWS); + AzPSUtils.pushPSModulePath(AzPSConstants.DEFAULT_AZ_PATH_ON_WINDOWS); break; case "macos": case "darwin": @@ -41,7 +47,7 @@ export default class AzPSConfig { static async importLatestAzAccounts() { let importLatestAccountsScript: string = AzPSScriptBuilder.getImportLatestModuleScript(AzPSConstants.AzAccounts); core.debug(`The script to import the latest Az.Accounts: ${importLatestAccountsScript}`); - let azAccountsPath: string = await AzPSConfig.runPSScript(importLatestAccountsScript); + let azAccountsPath: string = await AzPSUtils.runPSScript(importLatestAccountsScript); core.debug(`The latest Az.Accounts used: ${azAccountsPath}`); } @@ -76,4 +82,4 @@ export default class AzPSConfig { } return result.Result; } -} \ No newline at end of file +} diff --git a/src/common/Utils.ts b/src/common/Utils.ts index 63ec3aa80..9a2734be8 100644 --- a/src/common/Utils.ts +++ b/src/common/Utils.ts @@ -2,8 +2,7 @@ import * as core from '@actions/core'; import * as exec from '@actions/exec'; import * as io from '@actions/io'; import * as crypto from 'crypto'; -import AzPSConstants from '../PowerShell/AzPSConstants'; -import AzPSConfig from '../PowerShell/AzPSConfig'; +import { AzPSConstants, AzPSUtils } from '../PowerShell/AzPSUtils'; export function setUserAgent(): void { let usrAgentRepo = crypto.createHash('sha256').update(`${process.env.GITHUB_REPOSITORY}`).digest('hex'); @@ -29,8 +28,8 @@ export async function cleanupAzPSAccounts(): Promise { } core.debug(`PowerShell path: ${psPath}`); core.debug("Importing Azure PowerShell module."); - AzPSConfig.setPSModulePathForGitHubRunner(); - await AzPSConfig.importLatestAzAccounts(); + AzPSUtils.setPSModulePathForGitHubRunner(); + await AzPSUtils.importLatestAzAccounts(); core.info("Clearing azure powershell accounts from the local cache."); await exec.exec(`"${psPath}"`, ["-Command", "Clear-AzContext", "-Scope", "Process"]); await exec.exec(`"${psPath}"`, ["-Command", "Clear-AzContext", "-Scope", "CurrentUser", "-Force", "-ErrorAction", "SilentlyContinue"]);