Skip to content

Commit

Permalink
add workingDirectory support (#8)
Browse files Browse the repository at this point in the history
* add workingDirectory support

* fix typo
  • Loading branch information
JonathanSerafini authored Jul 12, 2023
1 parent ccc1a97 commit e5a6ad6
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@ When this parameter is specified, you must also provide a `scan_label` to identi

The optional timeout after which the Github check will be marked as failed. This defaults to 120 seconds.

### `workingDirectory` (Optional, string)

Optional path to change into before executing any commands.

7 changes: 6 additions & 1 deletion source/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import * as task from "azure-pipelines-task-lib/task"
import * as scanner from "./scanner"
import { BoostParams } from "./params"

async function run() {
export async function run() {
try {
const params = new BoostParams(process.env, task)
scanner.validateEnv(process.env, params)

if (params.workingDirectory) {
process.chdir(params.workingDirectory)
}

await scanner.downloadCLI(params)
await scanner.executeCLI(params)
} catch (err: unknown) {
Expand Down
3 changes: 3 additions & 0 deletions source/src/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const BoostParamEnvMap: BoostParamEnvMap = {
scanPath: "BOOST_SCAN_PATH",
scanTimeout: "BOOST_DIFF_SCAN_TIMEOUT",
tmpDir: "BOOST_TMP_DIR",
workingDirectory: "BOOST_WORKING_DIRECTORY",
}

class BoostParamsVars {
Expand All @@ -55,6 +56,7 @@ class BoostParamsVars {
scanPath: string | undefined = ""
scanTimeout: string | undefined = ""
tmpDir: string = ""
workingDirectory: string | undefined = ""
}

export class BoostParams extends BoostParamsVars {
Expand Down Expand Up @@ -105,6 +107,7 @@ export class BoostParams extends BoostParamsVars {
this.scannerId = tl.getInput("scannerId")
this.scanPath = tl.getInput("scanPath")
this.scanTimeout = tl.getInput("scanTimeout")
this.workingDirectory = tl.getInput("workingDirectory")
}

public loadEnv(env: any) {
Expand Down
7 changes: 7 additions & 0 deletions source/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@
"type": "int",
"defaultValue": "120",
"helpMarkDown": ""
},
{
"name": "workingDirectory",
"label": "working directory",
"type": "string",
"defaultValue": "",
"helpMarkDown": ""
}
],
"execution": {
Expand Down
56 changes: 56 additions & 0 deletions source/tests/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as task from "azure-pipelines-task-lib/task"
import * as tool from "azure-pipelines-tool-lib/tool"
import * as fs from "fs"
import * as os from "os"
import * as path from "path"

import { run } from "../src/index"
import * as scanner from "../src/scanner"
import { BoostParams } from "../src/params"

const INITIAL_ENV = process.env

jest.mock("../src/scanner")

beforeEach(async () => {
const tmpdir = await fs.promises.mkdtemp(
path.resolve(os.tmpdir(), "boostsec-scanner-azure")
)

process.env = {
TMPDIR: tmpdir,
}
process.chdir = jest.fn()
})

afterEach(async () => {
jest.clearAllMocks()
jest.resetModules()
jest.restoreAllMocks()

if (process.env.TMPDIR !== undefined) {
await fs.promises.rm(process.env.TMPDIR, { recursive: true, force: true })
}

process.env = INITIAL_ENV
})

describe("run", () => {
test("run ignores workingDirectory by default", async () => {
await run()

expect(process.chdir).not.toHaveBeenCalled()
expect(scanner.downloadCLI).toHaveBeenCalled()
expect(scanner.executeCLI).toHaveBeenCalled()
})

test("run changes workingDirectory", async () => {
process.env.BOOST_WORKING_DIRECTORY = "/tmp"

await run()

expect(process.chdir).toHaveBeenCalledWith("/tmp")
expect(scanner.downloadCLI).toHaveBeenCalled()
expect(scanner.executeCLI).toHaveBeenCalled()
})
})
3 changes: 2 additions & 1 deletion source/tests/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const INITIAL_ENV = process.env

beforeEach(async () => {
const tmpdir = await fs.promises.mkdtemp(
path.resolve(os.tmpdir(), "boostsec-scanner-szure")
path.resolve(os.tmpdir(), "boostsec-scanner-azure")
)

process.env = {
Expand Down Expand Up @@ -62,6 +62,7 @@ describe("BoostParams", () => {
["BOOST_SCANNER_ID", "scannerId"],
["BOOST_SCAN_PATH", "scanPath"],
["BOOST_DIFF_SCAN_TIMEOUT", "scanTimeout"],
["BOOST_WORKING_DIRECTORY", "workingDirectory"],
]

test.each(GetInputTestCases)(
Expand Down
2 changes: 1 addition & 1 deletion source/tests/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jest.mock("azure-pipelines-tool-lib/tool")

beforeEach(async () => {
const tmpdir = await fs.promises.mkdtemp(
path.resolve(os.tmpdir(), "boostsec-scanner-szure")
path.resolve(os.tmpdir(), "boostsec-scanner-azure")
)

process.env = {
Expand Down

0 comments on commit e5a6ad6

Please sign in to comment.