Skip to content

Commit

Permalink
Merge pull request #32 from lf-lang/formatting
Browse files Browse the repository at this point in the history
Formatting check
  • Loading branch information
lhstrh authored Jun 8, 2023
2 parents d0e81d9 + 99480b2 commit 7adffc6
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 42 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This GitHub action recursively visits a given `search_dir`, finds every `.lf` fi
- uses: lf-lang/action-check-lf-files@main
with:
checkout_dir: 'lingua-franca' # Where the lingua-franca repo should be
check_mode: `compile` # Do `compile` (default) or `format` check
compiler_ref: 'master' # Which version of the compiler to use
delete_if_exists: false # Delete if `checkout_dir` already exists
exclude_dirs: '["failing", "experimental"]' # JSON array of directories not to visit
Expand Down
38 changes: 26 additions & 12 deletions __tests__/debug.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,29 @@ if (!quick) {
}

// Run the action in the same process. (NOTE: also useful for debugging!)
test('expect failure', async () => {
process.env['INPUT_CHECKOUT_DIR'] = 'gh-action-test-0'
process.env['INPUT_COMPILER_REF'] = 'master'
process.env['INPUT_DELETE_IF_EXISTS'] = 'true'
process.env['INPUT_SKIP_CLONE'] = String(quick)
process.env['INPUT_EXCLUDE_DIRS'] = '[]'
process.env['INPUT_SEARCH_DIR'] = '.'
process.env['INPUT_NO_COMPILE_FLAG'] = 'false'
const result = await run(true)
expect(result).toBe('One or more tests failed to compile')
}, 600000)

test('expect compile failure', async () => {
process.env['INPUT_CHECK_MODE'] = 'compile'
process.env['INPUT_CHECKOUT_DIR'] = 'gh-action-test-0'
process.env['INPUT_COMPILER_REF'] = 'master'
process.env['INPUT_DELETE_IF_EXISTS'] = 'true'
process.env['INPUT_SKIP_CLONE'] = String(quick)
process.env['INPUT_EXCLUDE_DIRS'] = '[]'
process.env['INPUT_SEARCH_DIR'] = '.'
process.env['INPUT_NO_COMPILE_FLAG'] = 'false'
const result = await run(true)
expect(result).toBe('1 file(s) failed compile check')
}, 600000)

// Run the action in the same process. (NOTE: also useful for debugging!)
test('expect formatting failure', async () => {
process.env['INPUT_CHECK_MODE'] = 'format'
process.env['INPUT_CHECKOUT_DIR'] = 'gh-action-test-3'
process.env['INPUT_COMPILER_REF'] = 'master'
process.env['INPUT_DELETE_IF_EXISTS'] = 'true'
process.env['INPUT_SKIP_CLONE'] = String(quick)
process.env['INPUT_EXCLUDE_DIRS'] = '[]'
process.env['INPUT_SEARCH_DIR'] = '.'
process.env['INPUT_NO_COMPILE_FLAG'] = 'false'
const result = await run(true)
expect(result).toBe('1 file(s) failed format check')
}, 600000)
1 change: 1 addition & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {quick} from './debug.test'

// Run the action as a subprocess.
test('exclude failing', () => {
process.env['INPUT_CHECK_MODE'] = 'compile'
process.env['INPUT_CHECKOUT_DIR'] = 'gh-action-test-1'
process.env['INPUT_COMPILER_REF'] = 'master'
process.env['INPUT_DELETE_IF_EXISTS'] = 'true'
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ name: 'Check Lingua Franca files'
description: 'Ensure that all Lingua Franca files compile'
author: 'Marten Lohstroh'
inputs:
check_mode:
required: false
description: '`compile` (default) or `format`'
default: "compile"
checkout_dir:
required: false
description: 'Where the lingua-franca repo should be'
Expand Down
61 changes: 47 additions & 14 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lf/src/Passing.lf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
target C

main reactor {
reaction(startup) {= printf("Hello World.\n"); =}
reaction(startup) {= printf("Hello World.\n"); =}
}
41 changes: 31 additions & 10 deletions src/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,34 @@ const exec = promisify(cp.exec)
// FIXME: allow wildcards?
export const skipDirs = ['node_modules', 'src-gen', 'fed-gen']

export async function checkAll(
export async function checkCompile(
dir: string,
noCompile: boolean
): Promise<boolean> {
let passed = true
): Promise<number> {
return checkAll(
dir,
async filePath =>
await exec(`lfc ${noCompile ? '--no-compile' : ''} "${filePath}"`, {
env: process.env
})
)
}

export async function checkFormat(dir: string): Promise<number> {
return checkAll(
dir,
async filePath =>
await exec(`lff --check "${filePath}"`, {
env: process.env
})
)
}

async function checkAll(
dir: string,
cmd: (arg: string) => Promise<unknown>
): Promise<number> {
let failures = 0

const files = await readdir(dir)

Expand All @@ -25,21 +48,19 @@ export async function checkAll(
if (fileStats.isDirectory()) {
// Recursively traverse subdirectories
if (!skipDirs.includes(fileName)) {
passed = (await checkAll(filePath, noCompile)) && passed
failures += await checkAll(filePath, cmd)
}
} else if (fileName.endsWith('.lf')) {
// Invoke command on file
try {
await exec(`lfc ${noCompile ? '--no-compile' : ''} "${filePath}"`, {
env: process.env
})
await cmd(filePath)
core.info(`✔️ ${filePath}`)
} catch (error) {
core.info(`❌ ${filePath} (compilation failed)`)
core.info(`❌ ${filePath}`)
core.error(String(error))
passed = false
failures++
}
}
}
return passed
return failures
}
31 changes: 27 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import * as core from '@actions/core'
import {configurePath, deleteIfExists, clone, gradleStop} from './build'
import {skipDirs, checkAll} from './check'
import {skipDirs, checkCompile, checkFormat} from './check'

// eslint-disable-next-line no-shadow
enum Mode {
Compile = 'compile',
Format = 'format'
}

export async function run(softError = false): Promise<string> {
let result = 'Success'
const mode =
core.getInput('check_mode') === Mode.Format ? Mode.Format : Mode.Compile
const dir = core.getInput('checkout_dir')
const excludes: string[] = JSON.parse(core.getInput('exclude_dirs'))
const ref = core.getInput('compiler_ref')
Expand Down Expand Up @@ -33,8 +41,18 @@ export async function run(softError = false): Promise<string> {
for (const exclude of excludes) {
skipDirs.push(exclude)
}
if ((await checkAll(searchDir, noCompile)) === false) {
result = 'One or more tests failed to compile'
let fails = 0
switch (mode) {
case Mode.Compile:
fails = await checkCompile(searchDir, noCompile)
break
case Mode.Format:
fails = await checkFormat(searchDir)
break
}

if (fails > 0) {
result = `${fails} file(s) failed ${mode} check`
if (!softError) {
core.setFailed(result)
}
Expand All @@ -54,4 +72,9 @@ export async function run(softError = false): Promise<string> {
if (process.env['NODE_ENV'] !== 'test' || process.env['MAIN_DO_RUN'] === 'true')
run()
if (process.env['NODE_ENV'] === 'test')
skipDirs.push('gh-action-test-0', 'gh-action-test-1', 'gh-action-test-2')
skipDirs.push(
'gh-action-test-0',
'gh-action-test-1',
'gh-action-test-2',
'gh-action-test-3'
)

0 comments on commit 7adffc6

Please sign in to comment.