Skip to content

Commit

Permalink
Fix tealang CLI segfault
Browse files Browse the repository at this point in the history
* Add a CLI unit test
* Closes #6
  • Loading branch information
pzbitskiy committed Jun 26, 2021
1 parent bf90204 commit 24ee125
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
11 changes: 7 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Syntax highlighter for vscode: https://github.com/pzbitskiy/tealang-syntax-highl
if inFile == "-" {
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Printf(err.Error())
fmt.Println(err.Error())
os.Exit(1)
}
source = string(data)
Expand Down Expand Up @@ -124,10 +124,9 @@ Syntax highlighter for vscode: https://github.com/pzbitskiy/tealang-syntax-highl
fmt.Println(err.Error())
os.Exit(1)
}
bytecode = op.Program
}

bytecode = op.Program

if stdout {
output := teal
if bytecode != nil {
Expand Down Expand Up @@ -183,14 +182,18 @@ Syntax highlighter for vscode: https://github.com/pzbitskiy/tealang-syntax-highl
},
}

func main() {
func setRootCmdFlags() {
rootCmd.Flags().StringVarP(&outFile, "output", "o", "", "write output to this file")
rootCmd.Flags().BoolVarP(&compileOnly, "compile", "c", false, "compile to TEAL assembler, do not produce bytecode")
rootCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
rootCmd.Flags().StringVarP(&oneliner, "oneliner", "l", "", "compile logic one-liner like '(txn.Sender == \"abc\") && (1+2) >= 3'")
rootCmd.Flags().BoolVarP(&stdout, "stdout", "s", false, "write output to stdout instead of a file")
rootCmd.Flags().BoolVarP(&raw, "raw", "r", false, "do not hex-encode bytecode when outputting to stdout")
rootCmd.Flags().StringVarP(&dryrun, "dryrun", "d", "", "dry run program with transaction data from the file provided")
}

func main() {
setRootCmdFlags()

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
Expand Down
30 changes: 30 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"io/ioutil"
"os"
"testing"

"github.com/stretchr/testify/require"
)

func TestMainBasic(t *testing.T) {
setRootCmdFlags()

old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
defer func() {
os.Stdout = old
}()

rootCmd.SetArgs([]string{"-c", "examples/basic.tl", "-s"})

err := rootCmd.Execute()
require.NoError(t, err)
w.Close()

out, err := ioutil.ReadAll(r)
require.NoError(t, err)
require.Contains(t, string(out), "end_main")
}

0 comments on commit 24ee125

Please sign in to comment.