Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error handling for file loading #353

Merged
merged 5 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
push:
branches:
- main

name: build

jobs:
Expand All @@ -24,7 +24,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '17'
node-version: "17"
- run: cd ./tntc && npm install
- run: cd ./tntc && npm run compile
- run: cd ./tntc && npm test
Expand All @@ -39,19 +39,24 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '17'
node-version: "17"
- run: npm install -g txm
- run: cd ./tntc && npm install
- run: cd ./tntc && npm run compile && npm link
- run: cd ./tntc && txm cli-tests.md
- name: Blackbox integration tests
run: cd ./tntc && txm cli-tests.md
- name: Blackbox integration tests with I/O
# This tests fail on windows currently
# See https://github.com/anko/txm/issues/10
run: cd ./tntc && txm io-cli-tests.md
if: matrix.operating-system != 'windows-latest'

tntc-antlr-grammar:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '17'
node-version: "17"
- run: cd ./tntc && npm install
- run: cd ./tntc && npm run antlr

2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ true
[Installing the VSCode plugin]: https://github.com/informalsystems/tnt/blob/main/vscode/tnt/README.md#temp-how-to-run-it-locally
[Language server protocol]: https://microsoft.github.io/language-server-protocol/
[tntc unit tests]: https://github.com/informalsystems/tnt/blob/main/tntc/README.md#unit-tests
[tntc integration tests]: https://github.com/informalsystems/tnt/blob/main/tntc/README.md#integration-tests
[tntc integration tests]: ./tntc/README.md#integration-tests
[Mocha]: https://mochajs.org/
[Chai]: https://www.chaijs.com/
[txm]: https://www.npmjs.com/package/txm
Expand Down
7 changes: 6 additions & 1 deletion tntc/cli-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ installed:
npm install -g txm
```

*NOTE*: these tests only check that particular invocations succeed or fail. For
tests that examine particular output, use
[./io-cli-tests.md](./io-cli-tests.md).


### OK on parse Paxos

This command parses the Paxos example.
Expand Down Expand Up @@ -352,4 +357,4 @@ tntc typecheck ../examples/tuples.tnt
```

<!-- !test check tuples - Types & Effects-->
expect exit code 0
expect exit code 0
30 changes: 30 additions & 0 deletions tntc/io-cli-tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
This is a suite of blackbox integration tests for the `tntc` executable.
The tests in this file check that particular output is produced when
particular input is received.

These tests require [`txm`](https://www.npmjs.com/package/txm) to be installed:

```sh
npm install -g txm
```

All of the test inputs in the following test cases are commands executed by `bash`.

<!-- !test program
bash -
-->

### User error on parse with non-existent file

Regression test for [#215](https://github.com/informalsystems/tnt/issues/215).
We want to ensure we do not throw uncaught exceptions when the input file is
doesn't exist.

<!-- !test in non-existent file -->
tntc parse ../examples/non-existent.file

<!-- !test exit 1 -->
<!-- !test err non-existent file -->
error: file ../examples/non-existent.file does not exist


2 changes: 1 addition & 1 deletion tntc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"compile": "tsc",
"test": "mocha -r ts-node/register test/*.test.ts test/**/*.test.ts",
"coverage": "nyc npm run test",
"integration": "txm --no-color cli-tests.md",
"integration": "txm cli-tests.md && txm io-cli-tests.md",
"antlr": "antlr4ts -visitor ./src/generated/Tnt.g4 && antlr4ts -visitor ./src/generated/Effect.g4",
"format": "eslint --fix '**/*.ts'"
},
Expand Down
31 changes: 25 additions & 6 deletions tntc/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Igor Konnov, Gabriela Moreira, Informal Systems, 2021-2022
*/

import { readFileSync, writeFileSync } from 'fs'
import { existsSync, PathLike, readFileSync, writeFileSync } from 'fs'
import { resolve } from 'path'
import { cwd } from 'process'
import { lf } from 'eol'
Expand All @@ -29,6 +29,7 @@ import { tntRepl } from './repl'
import { inferTypes } from './types/inferrer'
import { effectToString } from './effects/printing'
import { typeSchemeToString } from './types/printing'
import { Either, right, left } from '@sweet-monads/either'

/**
* Parse a TNT specification.
Expand Down Expand Up @@ -103,18 +104,36 @@ function runRepl(_argv: any) {
tntRepl(process.stdin, process.stdout)
}

// Load a file into a string
function loadFile(p: PathLike): Either<string, string> {
if (existsSync(p)) {
try {
return right(readFileSync(p, 'utf8'))
} catch (err: unknown) {
return left(`error: file ${p} could not be opened due to ${err}`)
}
} else {
return left(`error: file ${p} does not exist`)
}
}

// read either the standard input or an input file
function parseModule(argv: any): [Phase1Result, LookupTableByModule, string] {
const data = readFileSync(argv.input, 'utf8')
return parseText(argv, lf(data))
const res = loadFile(argv.input)
if (res.isRight()) {
return parseText(argv, res.value)
} else {
console.error(res.value)
process.exit(1)
}
Comment on lines +122 to +128
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a nice way to handle the errors here: instead, we should chance the error-prone computations, and have error reporting at the outermost layer. But I'm introducing the change in this way just to open the conversation.

If anyone feels strongly about avoiding use of Either to faciliate composition of our error-apt computations here, I can revert this easily.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should totally use Either here. There are some other parse and name resolution functions that return kind: 'ok' or kind: 'error' objects instead of Either because we wrote them before introducing the Either library. We should update all of them at some point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent. I'll swap this over while I'm refactoring to get the type map out. Thanks for the guidance and confirmation :)

}

// a callback to parse the text that we get from readFile
function parseText(argv: any, text: string): [Phase1Result, LookupTableByModule, string] {
const path = resolve(cwd(), argv.input)
const phase1Result = parsePhase1(text, path)
if (phase1Result.kind === 'error') {
reportError(argv, text, phase1Result)
reportParseError(argv, text, phase1Result)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just an incidental change to document the nature of this function, which is not suitable for general purpose error reporting.

process.exit(1)
}

Expand All @@ -125,7 +144,7 @@ function parseText(argv: any, text: string): [Phase1Result, LookupTableByModule,

const phase2Result = parsePhase2(phase1Result.module, phase1Result.sourceMap)
if (phase2Result.kind === 'error') {
reportError(argv, text, phase2Result)
reportParseError(argv, text, phase2Result)
process.exit(1)
}

Expand All @@ -141,7 +160,7 @@ function parseText(argv: any, text: string): [Phase1Result, LookupTableByModule,
return [phase1Result, phase2Result.table, text]
}

function reportError(argv: any, sourceCode: string, result: { kind: 'error', messages: ErrorMessage[] }) {
function reportParseError(argv: any, sourceCode: string, result: { kind: 'error', messages: ErrorMessage[] }) {
if (argv.out) {
// write the errors to the output file
writeToJson(argv.out, result)
Expand Down