Skip to content

Commit

Permalink
Merge pull request #649 from ottofeller/rust-working-directory
Browse files Browse the repository at this point in the history
  • Loading branch information
gvidon authored Jul 21, 2024
2 parents b29564a + a4f579c commit bdd4b50
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/common/github/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,40 @@ describe('GitHub utils', () => {
expect(env!.RUSTFLAGS).toEqual('-D unused_crate_dependencies')
})

test('runs at root if no outdir is configured', () => {
const project = new TestProject()
new RustTestWorkflow(project.github!)
const snapshot = synthSnapshot(project)
const workflow = YAML.parse(snapshot[workflowPath])
const noArgsJobs = ['check', 'test']

noArgsJobs.forEach((job) => {
const {args} = workflow.jobs[job].steps.at(-1)!.with
expect(args).toBeUndefined()
})

const jobsWithArgs = ['clippy', 'format']

jobsWithArgs.forEach((job) => {
const {args} = workflow.jobs[job].steps.at(-1)!.with
expect(args).not.toContain(`--manifest-path`)
})
})

test('runs for a nested project if outdir is configured', () => {
const project = new TestProject()
const outdir = 'inner-project'
new RustTestWorkflow(project.github!, {rootdir: outdir})
const snapshot = synthSnapshot(project)
const workflow = YAML.parse(snapshot[workflowPath])
const jobs = ['clippy', 'check', 'format', 'test']

jobs.forEach((job) => {
const {args} = workflow.jobs[job].steps.at(-1)!.with
expect(args).toContain(`--manifest-path=${outdir}/Cargo.toml`)
})
})

describe('addToProject', () => {
test('does nothing by default', () => {
const project = new TestProjectWithRustTestWorkflow({})
Expand Down
17 changes: 16 additions & 1 deletion src/common/github/rust-test-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export interface RustTestWorkflowOptions extends GithubWorkflowOptions {
*/
readonly name?: string

/**
* The root directory of the rust project.
*
* This directory is expected to contain the manifest file.
*
* @default "."
*/
readonly rootdir?: string

/**
* A list of paths on pushes to which the workflow will run.
* @default ['.']
Expand Down Expand Up @@ -76,6 +85,8 @@ export class RustTestWorkflow extends GithubWorkflow {

const paths = options.triggerOnPaths
const branches = options.triggerOnBranches ?? ['main']
const {rootdir} = options
const manifestPath = rootdir ? `--manifest-path=${rootdir}/Cargo.toml` : undefined

this.on({
pullRequest: {paths, types: ['opened', 'synchronize']},
Expand Down Expand Up @@ -114,7 +125,11 @@ export class RustTestWorkflow extends GithubWorkflow {
{
name: `Run ${command}`,
uses: 'actions-rs/cargo@v1',
with: {command, args, toolchain},
with: {
command,
args: [manifestPath, args].filter(Boolean).join(' ') || undefined,
toolchain,
},
env,
},
],
Expand Down

0 comments on commit bdd4b50

Please sign in to comment.