From 5b6924f74788c5b513b55c8bd66a11d729d6f090 Mon Sep 17 00:00:00 2001 From: Elliot Chance Date: Mon, 5 Jun 2017 07:36:14 +1000 Subject: [PATCH 1/7] =?UTF-8?q?Run=20all=20integrations=20tests=20internal?= =?UTF-8?q?ly=20with=20=E2=80=9Cgo=20test"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 4 ++++ main_test.go | 41 ++++++++++++++++++++++++++++++---------- program/program.go | 2 ++ transpiler/transpiler.go | 23 ++++++++++++++++++++++ 4 files changed, 60 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 8280cff28..b33955cc4 100644 --- a/main.go +++ b/main.go @@ -31,6 +31,9 @@ type ProgramArgs struct { inputFile string outputFile string packageName string + + // A private option to output the Go as a *_test.go file. + outputAsTest bool } func readAST(data []byte) []string { @@ -180,6 +183,7 @@ func Start(args ProgramArgs) { p := program.NewProgram() p.Verbose = args.verbose + p.OutputAsTest = true // args.outputAsTest err = transpiler.TranspileAST(args.inputFile, args.packageName, p, tree[0].(ast.Node)) if err != nil { diff --git a/main_test.go b/main_test.go index ffd24cf21..5d42b2dbb 100644 --- a/main_test.go +++ b/main_test.go @@ -84,20 +84,27 @@ func TestIntegrationScripts(t *testing.T) { programArgs := ProgramArgs{ inputFile: file, - outputFile: "build/main.go", + outputFile: "build/main_test.go", packageName: "main", + + // This appends a TestApp function to the output source so we + // can run "go test" against the produced binary. + outputAsTest: true, } // Compile Go Start(programArgs) - buildErr, err := exec.Command("go", "build", "-o", goPath, "build/main.go").CombinedOutput() - if err != nil { - t.Fatal(string(buildErr), err) - } + // buildErr, err := exec.Command( + // "go", "test", programArgs.outputFile, + // "--", args...).CombinedOutput() + // if err != nil { + // t.Fatal(string(buildErr), err) + // } - // Run Go program - cmd = exec.Command(goPath, args...) + // Run Go program. The "-v" option is important; without it most or + // all of the fmt.* output would be supressed. + cmd = exec.Command("go", "test", "-v", programArgs.outputFile, "--", "some", "args") cmd.Stdin = strings.NewReader(stdin) cmd.Stdout = &goProgram.stdout cmd.Stderr = &goProgram.stderr @@ -128,9 +135,23 @@ func TestIntegrationScripts(t *testing.T) { } // Check stdout - if cProgram.stdout.String() != goProgram.stdout.String() { - t.Fatalf(util.ShowDiff(cProgram.stdout.String(), - goProgram.stdout.String())) + cOut := cProgram.stdout.String() + goOutLines := strings.Split(goProgram.stdout.String(), "\n") + + // Skip the first line. + if goOutLines[0] != "=== RUN TestApp" { + t.Fatalf("The first line from the Go stdout is incorrect.") + } + + // Skip the last two lines. + if !strings.HasPrefix(goOutLines[len(goOutLines)-2], "ok \tcommand-line-arguments") { + t.Fatalf("The second last line from the Go stdout is incorrect.") + } + + goOut := strings.Join(goOutLines[1:len(goOutLines)-2], "\n") + "\n" + + if cOut != goOut { + t.Fatalf(util.ShowDiff(cOut, goOut)) } // If this is not an example we will extact the number of tests run. diff --git a/program/program.go b/program/program.go index 2f2a14360..cdc826258 100644 --- a/program/program.go +++ b/program/program.go @@ -58,6 +58,8 @@ type Program struct { // A map of all the global variables (variables that exist outside of a // function) and their types. GlobalVariables map[string]string + + OutputAsTest bool } // NewProgram creates a new blank program. diff --git a/transpiler/transpiler.go b/transpiler/transpiler.go index 3ed46a339..648bf7e80 100644 --- a/transpiler/transpiler.go +++ b/transpiler/transpiler.go @@ -29,6 +29,29 @@ func TranspileAST(fileName, packageName string, p *program.Program, root ast.Nod // Now begin building the Go AST. err = transpileToNode(root, p) + if p.OutputAsTest { + p.AddImport("testing") + + // TODO: There should be a cleaner way to add a function to the program. + // This code was taken from the end of transpileFunctionDecl. + p.File.Decls = append(p.File.Decls, &goast.FuncDecl{ + Name: util.NewIdent("TestApp"), + Type: &goast.FuncType{ + Params: &goast.FieldList{ + List: []*goast.Field{ + &goast.Field{ + Names: []*goast.Ident{util.NewIdent("t")}, + Type: util.NewTypeIdent("*testing.T"), + }, + }, + }, + }, + Body: &goast.BlockStmt{ + List: []goast.Stmt{util.NewExprStmt(util.NewCallExpr("main"))}, + }, + }) + } + // Now we need to build the __init() function. This sets up certain state // and variables that the runtime expects to be ready. p.File.Decls = append(p.File.Decls, &goast.FuncDecl{ From 2a02d93b7dda1bc2cf1199c7ecad7f9f271f2593 Mon Sep 17 00:00:00 2001 From: Elliot Chance Date: Sun, 13 Aug 2017 14:58:58 +1000 Subject: [PATCH 2/7] =?UTF-8?q?All=20tests=20now=20running=20through=20?= =?UTF-8?q?=E2=80=9Cgo=20test=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main_test.go | 80 +++++++++++++++++++++++++++----------- noarch/stdio.go | 16 +++++++- tests/argv.c | 20 ++++++++-- tests/getchar.c | 3 -- tests/stdio.c | 4 +- transpiler/declarations.go | 10 +---- transpiler/transpiler.go | 23 ++++++++++- 7 files changed, 114 insertions(+), 42 deletions(-) diff --git a/main_test.go b/main_test.go index a3dc71dd8..502fec976 100644 --- a/main_test.go +++ b/main_test.go @@ -34,7 +34,7 @@ type programOut struct { // // You can also run a single file with: // -// go test -tags=integration -run=TestIntegrationScripts/tests/ctype/isalnum.c +// go test -tags=integration -run=TestIntegrationScripts/tests/ctype.c // func TestIntegrationScripts(t *testing.T) { testFiles, err := filepath.Glob("tests/*.c") @@ -110,15 +110,10 @@ func TestIntegrationScripts(t *testing.T) { t.Fatalf("error: %s\n%s", err, out) } - //buildErr, err := exec.Command("go", "build", "-o", goPath, subFolder+mainFileName).CombinedOutput() - //if err != nil { - // t.Fatal(string(buildErr), err) - //} - // Run Go program. The "-v" option is important; without it most or - // all of the fmt.* output would be supressed. + // all of the fmt.* output would be suppressed. cmd = exec.Command("go", "test", "-v", programArgs.outputFile, "--", "some", "args") - cmd.Stdin = strings.NewReader(stdin) + cmd.Stdin = strings.NewReader(strings.Repeat("7", 1000)) cmd.Stdout = &goProgram.stdout cmd.Stderr = &goProgram.stderr err = cmd.Run() @@ -132,14 +127,6 @@ func TestIntegrationScripts(t *testing.T) { } } - // Check if both exit codes are zero (or non-zero) - if cProgram.isZero != goProgram.isZero { - t.Fatalf("Exit statuses did not match.\n" + - util.ShowDiff(cProgram.stdout.String(), - goProgram.stdout.String()), - ) - } - // Check stderr if cProgram.stderr.String() != goProgram.stderr.String() { t.Fatalf("Expected %q, Got: %q", @@ -151,17 +138,66 @@ func TestIntegrationScripts(t *testing.T) { cOut := cProgram.stdout.String() goOutLines := strings.Split(goProgram.stdout.String(), "\n") - // Skip the first line. + // An out put should look like this: + // + // === RUN TestApp + // 1..3 + // 1 ok - argc == 3 + offset + // 2 ok - argv[1 + offset] == "some" + // 3 ok - argv[2 + offset] == "args" + // ok command-line-arguments 0.005s + // + // The first and last line can be ignored as they are part of the go + // test suite and not part of the program output. + // + // Note: There is a blank line at the end of the output so when we + // say the last line we are really talking about the second last + // line. Rather than trimming the whitespace off the C and Go output + // we will just make note of the different line index. + // + // Some tests are designed to fail, like assert.c. In this case the + // result output is slightly different: + // + // === RUN TestApp + // 1..0 + // 10 + // exit status 134 + // FAIL command-line-arguments 0.006s + // + // The last two lines need to be removed. + // + // Before we proceed comparing the raw output we should check that + // the header and footer of the output fits one of the two formats + // in the examples above. if goOutLines[0] != "=== RUN TestApp" { - t.Fatalf("The first line from the Go stdout is incorrect.") + t.Fatalf("The header of the output cannot be understood:\n%s", + strings.Join(goOutLines, "\n")) + } + if !strings.HasPrefix(goOutLines[len(goOutLines)-2], "ok \tcommand-line-arguments") && + !strings.HasPrefix(goOutLines[len(goOutLines)-2], "FAIL\tcommand-line-arguments") { + t.Fatalf("The footer of the output cannot be understood:\n%v", + strings.Join(goOutLines, "\n")) } - // Skip the last two lines. - if !strings.HasPrefix(goOutLines[len(goOutLines)-2], "ok \tcommand-line-arguments") { - t.Fatalf("The second last line from the Go stdout is incorrect.") + //panic(goOutLines) + + // A failure will cause (always?) "go test" to output the exit code + // before the final line. We should also ignore this as its not part + // of our output. + // + // There is a separate check to see that both the C and Go programs + // return the same exit code. + removeLinesFromEnd := 2 + if strings.HasPrefix(goOutLines[len(goOutLines)-3], "exit status") { + removeLinesFromEnd = 3 } - goOut := strings.Join(goOutLines[1:len(goOutLines)-2], "\n") + "\n" + goOut := strings.Join(goOutLines[1:len(goOutLines)-removeLinesFromEnd], "\n") + "\n" + + // Check if both exit codes are zero (or non-zero) + if cProgram.isZero != goProgram.isZero { + t.Fatalf("Exit statuses did not match.\n%s", util.ShowDiff(cOut, goOut)) + } if cOut != goOut { t.Fatalf(util.ShowDiff(cOut, goOut)) diff --git a/noarch/stdio.go b/noarch/stdio.go index 48ebe12d6..bbb066711 100644 --- a/noarch/stdio.go +++ b/noarch/stdio.go @@ -8,6 +8,15 @@ import ( "reflect" ) +// Programs generated by c2go will reference noarch.Stdin instead of os.Stdin +// directly so that under test these can be replaced. This is required because +// "go test" does not redirect the stdin to the executable it is testing. +var ( + Stdin = NewFile(os.Stdin) + Stdout = NewFile(os.Stdout) + Stderr = NewFile(os.Stderr) +) + // File represents the definition has been translated from the original // definition for __sFILE, which is an alias for FILE. Not all of the attributes // have been translated. They should be turned on as needed. @@ -470,7 +479,7 @@ func Fputc(c int, f *File) int { // // It is equivalent to calling getc with stdin as argument. func Getchar() int { - return getc(os.Stdin) + return getc(Stdin.OsFile) } // Fseek handles fseek(). @@ -659,7 +668,10 @@ func Puts(str []byte) int { // string. func Scanf(format []byte, args ...interface{}) int { realArgs := prepareArgsForScanf(args) - n, _ := fmt.Scanf(CStringToString(format), realArgs...) + + // We cannot use fmt.Scanf() here because that would use the real stdin + // which does not work under test. See docs for noarch.Stdin. + n, _ := fmt.Fscanf(Stdin.OsFile, CStringToString(format), realArgs...) finalizeArgsForScanf(realArgs, args) return n diff --git a/tests/argv.c b/tests/argv.c index 42b7fcd6f..c16e457c9 100644 --- a/tests/argv.c +++ b/tests/argv.c @@ -7,14 +7,26 @@ int main(int argc, const char **argv) { plan(3); - is_eq(argc, 3); + // When this file is converted to go it is run through "go test" that needs + // some extra arguments before the standard C arguments. We need to adjust + // an offset so that the C program and the Go program read the same index + // for the first index of the real arguments. + int offset = 0; - // We cannot compare the zeroth argument becuase it will be different for C + // More than three arguments means it must be run under "go test". If not + // the assertion immediately below will fail. + if (argc > 3) { + offset = 2; + } + + is_eq(argc, 3 + offset); + + // We cannot compare the zeroth argument because it will be different for C // and Go. // is_streq(argv[0], "build/go.out"); - is_streq(argv[1], "some"); - is_streq(argv[2], "args"); + is_streq(argv[1 + offset], "some"); + is_streq(argv[2 + offset], "args"); done_testing(); } diff --git a/tests/getchar.c b/tests/getchar.c index 1ea759104..3539ec23f 100644 --- a/tests/getchar.c +++ b/tests/getchar.c @@ -8,11 +8,8 @@ int main() plan(1); int c; - diag("Enter text. Include a dot ('.') in a sentence to exit:"); c = getchar(); is_eq(c, '7'); - putchar(c); - done_testing(); } diff --git a/tests/stdio.c b/tests/stdio.c index 016dca34f..9cd96546d 100644 --- a/tests/stdio.c +++ b/tests/stdio.c @@ -298,7 +298,7 @@ void test_fread() // obtain file size: fseek(pFile, 0, SEEK_END); lSize = ftell(pFile); - is_eq(lSize, 301); + is_eq(lSize, 216); rewind(pFile); @@ -309,7 +309,7 @@ void test_fread() // See issue #107 buffer[lSize - 1] = 0; - is_eq(strlen(buffer), 300); + is_eq(strlen(buffer), 215); // terminate fclose(pFile); diff --git a/transpiler/declarations.go b/transpiler/declarations.go index b0c15197a..3966c478c 100644 --- a/transpiler/declarations.go +++ b/transpiler/declarations.go @@ -227,10 +227,7 @@ func transpileVarDecl(p *program.Program, n *ast.VarDecl) ( util.NewBinaryExpr( goast.NewIdent(name), token.ASSIGN, - util.NewCallExpr( - "noarch.NewFile", - util.NewTypeIdent("os."+util.Ucfirst(name[2:len(name)-1])), - ), + util.NewTypeIdent("noarch."+util.Ucfirst(name[2:len(name)-1])), "*noarch.File", true, ), @@ -244,10 +241,7 @@ func transpileVarDecl(p *program.Program, n *ast.VarDecl) ( util.NewBinaryExpr( goast.NewIdent(name), token.ASSIGN, - util.NewCallExpr( - "noarch.NewFile", - util.NewTypeIdent("os."+util.Ucfirst(name)), - ), + util.NewTypeIdent("noarch."+util.Ucfirst(name)), theType, true, ), diff --git a/transpiler/transpiler.go b/transpiler/transpiler.go index bbb64c893..11b9fa98f 100644 --- a/transpiler/transpiler.go +++ b/transpiler/transpiler.go @@ -31,6 +31,7 @@ func TranspileAST(fileName, packageName string, p *program.Program, root ast.Nod if p.OutputAsTest { p.AddImport("testing") + p.AddImport("io/ioutil") // TODO: There should be a cleaner way to add a function to the program. // This code was taken from the end of transpileFunctionDecl. @@ -47,7 +48,27 @@ func TranspileAST(fileName, packageName string, p *program.Program, root ast.Nod }, }, Body: &goast.BlockStmt{ - List: []goast.Stmt{util.NewExprStmt(util.NewCallExpr("main"))}, + List: []goast.Stmt{ + // TODO: docs + util.NewExprStmt(&goast.Ident{Name: "os.Chdir(\"../../..\")"}), + + // "go test" does not redirect stdin to the executable + // running the test so we need to override them in the test + // itself. See documentation for noarch.Stdin. + util.NewExprStmt(&goast.Ident{Name: "ioutil.WriteFile(\"build/stdin\", []byte{'7'}, 0777)"}), + util.NewExprStmt( + &goast.Ident{Name: "stdin, _ := os.Open(\"build/stdin\")"}, + ), + util.NewExprStmt(util.NewBinaryExpr( + &goast.Ident{Name: "noarch.Stdin"}, + token.ASSIGN, + &goast.Ident{Name: "noarch.NewFile(stdin)"}, + "*noarch.File", + true, + )), + + util.NewExprStmt(util.NewCallExpr("main")), + }, }, }) } From 6c36aab8e28da53403a9b3125b77d9dd158d2f77 Mon Sep 17 00:00:00 2001 From: Elliot Chance Date: Sun, 13 Aug 2017 16:32:20 +1000 Subject: [PATCH 3/7] Merge code coverage for stdlib packages --- main_test.go | 48 +++++++++++++++++++++++++++++++++--------------- tests/tests.h | 6 +++++- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/main_test.go b/main_test.go index 502fec976..b5059b0c1 100644 --- a/main_test.go +++ b/main_test.go @@ -112,7 +112,17 @@ func TestIntegrationScripts(t *testing.T) { // Run Go program. The "-v" option is important; without it most or // all of the fmt.* output would be suppressed. - cmd = exec.Command("go", "test", "-v", programArgs.outputFile, "--", "some", "args") + testName := strings.Split(file, ".")[0][6:] + cmd = exec.Command( + "go", "test", + "-v", + "-race", + "-covermode", "count", + "-coverprofile", testName+".coverprofile", + "-coverpkg", "./noarch,./linux,./darwin", + programArgs.outputFile, + "--", "some", "args", + ) cmd.Stdin = strings.NewReader(strings.Repeat("7", 1000)) cmd.Stdout = &goProgram.stdout cmd.Stderr = &goProgram.stderr @@ -127,11 +137,17 @@ func TestIntegrationScripts(t *testing.T) { } } - // Check stderr - if cProgram.stderr.String() != goProgram.stderr.String() { - t.Fatalf("Expected %q, Got: %q", - cProgram.stderr.String(), - goProgram.stderr.String()) + // Check stderr. "go test" will produce warnings when packages are + // not referenced as dependencies. We need to strip out these + // warnings so it doesn't effect the comparison. + cProgramStderr := cProgram.stderr.String() + goProgramStderr := goProgram.stderr.String() + + r := regexp.MustCompile("warning: no packages being tested depend on .+\n") + goProgramStderr = r.ReplaceAllString(goProgramStderr, "") + + if cProgramStderr != goProgramStderr { + t.Fatalf("Expected %q, Got: %q", cProgramStderr, goProgramStderr) } // Check stdout @@ -145,10 +161,13 @@ func TestIntegrationScripts(t *testing.T) { // 1 ok - argc == 3 + offset // 2 ok - argv[1 + offset] == "some" // 3 ok - argv[2 + offset] == "args" - // ok command-line-arguments 0.005s + // --- PASS: TestApp (0.03s) + // PASS + // coverage: 0.0% of statements + // ok command-line-arguments 1.050s // - // The first and last line can be ignored as they are part of the go - // test suite and not part of the program output. + // The first line and 4 of the last lines can be ignored as they are + // part of the "go test" runner and not part of the program output. // // Note: There is a blank line at the end of the output so when we // say the last line we are really talking about the second last @@ -161,10 +180,11 @@ func TestIntegrationScripts(t *testing.T) { // === RUN TestApp // 1..0 // 10 - // exit status 134 - // FAIL command-line-arguments 0.006s + // # FAILED: There was 1 failed tests. + // exit status 101 + // FAIL command-line-arguments 0.041s // - // The last two lines need to be removed. + // The last three lines need to be removed. // // Before we proceed comparing the raw output we should check that // the header and footer of the output fits one of the two formats @@ -179,15 +199,13 @@ func TestIntegrationScripts(t *testing.T) { strings.Join(goOutLines, "\n")) } - //panic(goOutLines) - // A failure will cause (always?) "go test" to output the exit code // before the final line. We should also ignore this as its not part // of our output. // // There is a separate check to see that both the C and Go programs // return the same exit code. - removeLinesFromEnd := 2 + removeLinesFromEnd := 5 if strings.HasPrefix(goOutLines[len(goOutLines)-3], "exit status") { removeLinesFromEnd = 3 } diff --git a/tests/tests.h b/tests/tests.h index b321328bc..98a5268ae 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -270,7 +270,11 @@ static int last_test_was_ok = 1; diag("FAILED: Expected %d tests, but ran %d.", total_tests, current_test); \ exit_status = 102; \ } \ - return exit_status; + /* If we exit (with any status) the Go code coverage will not be generated. */ \ + if (exit_status != 0) { \ + return exit_status; \ + } \ + return 0; // or_return will return (with an optional value provided) if the check failed. // From dfba2e349b2a9a82e14fcba30761c49d8ef7e36f Mon Sep 17 00:00:00 2001 From: Elliot Chance Date: Sun, 13 Aug 2017 16:50:50 +1000 Subject: [PATCH 4/7] Fixing mac tests --- main_test.go | 10 +++++----- tests/argv.c | 6 ++---- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/main_test.go b/main_test.go index b5059b0c1..a31eec95c 100644 --- a/main_test.go +++ b/main_test.go @@ -115,15 +115,15 @@ func TestIntegrationScripts(t *testing.T) { testName := strings.Split(file, ".")[0][6:] cmd = exec.Command( "go", "test", + programArgs.outputFile, "-v", "-race", - "-covermode", "count", - "-coverprofile", testName+".coverprofile", - "-coverpkg", "./noarch,./linux,./darwin", - programArgs.outputFile, + "-covermode=count", + "-coverprofile="+testName+".coverprofile", + "-coverpkg=./noarch,./linux,./darwin", "--", "some", "args", ) - cmd.Stdin = strings.NewReader(strings.Repeat("7", 1000)) + cmd.Stdin = strings.NewReader("7") cmd.Stdout = &goProgram.stdout cmd.Stderr = &goProgram.stderr err = cmd.Run() diff --git a/tests/argv.c b/tests/argv.c index c16e457c9..90790c3cb 100644 --- a/tests/argv.c +++ b/tests/argv.c @@ -5,7 +5,7 @@ int main(int argc, const char **argv) { - plan(3); + plan(2); // When this file is converted to go it is run through "go test" that needs // some extra arguments before the standard C arguments. We need to adjust @@ -16,11 +16,9 @@ int main(int argc, const char **argv) // More than three arguments means it must be run under "go test". If not // the assertion immediately below will fail. if (argc > 3) { - offset = 2; + offset = 3; } - is_eq(argc, 3 + offset); - // We cannot compare the zeroth argument because it will be different for C // and Go. // is_streq(argv[0], "build/go.out"); From 43e2412e6bfce0ae220ebeee7993c3f4a7c9d5e2 Mon Sep 17 00:00:00 2001 From: Elliot Chance Date: Sun, 13 Aug 2017 18:24:14 +1000 Subject: [PATCH 5/7] Output failure to parse TAP --- main_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/main_test.go b/main_test.go index a31eec95c..1bbfbff76 100644 --- a/main_test.go +++ b/main_test.go @@ -221,14 +221,16 @@ func TestIntegrationScripts(t *testing.T) { t.Fatalf(util.ShowDiff(cOut, goOut)) } - // If this is not an example we will extact the number of tests run. + // If this is not an example we will extract the number of tests + // run. if strings.Index(file, "examples/") == -1 && isVerbose { firstLine := strings.Split(goProgram.stdout.String(), "\n")[0] matches := regexp.MustCompile(`1\.\.(\d+)`). FindStringSubmatch(firstLine) if len(matches) == 0 { - t.Fatalf("Test did not output tap: %s", file) + t.Fatalf("Test did not output tap: %s, got:\n%s", file, + goProgram.stdout.String()) } fmt.Printf("TAP: # %s: %s tests\n", file, matches[1]) From 35df7544e47840bbdda17d6d3a46858833f58737 Mon Sep 17 00:00:00 2001 From: Elliot Chance Date: Sun, 13 Aug 2017 18:30:00 +1000 Subject: [PATCH 6/7] Fix TAP parser for integration tests --- main_test.go | 2 +- program/program.go | 5 ++++- transpiler/transpiler.go | 1 - 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/main_test.go b/main_test.go index 1bbfbff76..014ae51a0 100644 --- a/main_test.go +++ b/main_test.go @@ -224,7 +224,7 @@ func TestIntegrationScripts(t *testing.T) { // If this is not an example we will extract the number of tests // run. if strings.Index(file, "examples/") == -1 && isVerbose { - firstLine := strings.Split(goProgram.stdout.String(), "\n")[0] + firstLine := strings.Split(goOut, "\n")[0] matches := regexp.MustCompile(`1\.\.(\d+)`). FindStringSubmatch(firstLine) diff --git a/program/program.go b/program/program.go index 94595c40b..dc704bb17 100644 --- a/program/program.go +++ b/program/program.go @@ -58,7 +58,7 @@ type Program struct { Unions StructRegistry // If verbose is on progress messages will be printed immediately as code - // comments (so that they do not intefere with the program output). + // comments (so that they do not interfere with the program output). Verbose bool // Contains the messages (for example, "// Warning") generated when @@ -70,6 +70,9 @@ type Program struct { // function) and their types. GlobalVariables map[string]string + // This option is not available through the command line. It is to allow the + // internal integration testing to generate the output in the form of a + // Go-test rather than a standalone Go file. OutputAsTest bool } diff --git a/transpiler/transpiler.go b/transpiler/transpiler.go index 11b9fa98f..29200f70b 100644 --- a/transpiler/transpiler.go +++ b/transpiler/transpiler.go @@ -49,7 +49,6 @@ func TranspileAST(fileName, packageName string, p *program.Program, root ast.Nod }, Body: &goast.BlockStmt{ List: []goast.Stmt{ - // TODO: docs util.NewExprStmt(&goast.Ident{Name: "os.Chdir(\"../../..\")"}), // "go test" does not redirect stdin to the executable From fbe9f990650451a37a32acfb12b281d7258d3ec7 Mon Sep 17 00:00:00 2001 From: Elliot Chance Date: Sun, 13 Aug 2017 19:41:20 +1000 Subject: [PATCH 7/7] Fixing examples --- main_test.go | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/main_test.go b/main_test.go index 014ae51a0..532ad23fc 100644 --- a/main_test.go +++ b/main_test.go @@ -96,7 +96,7 @@ func TestIntegrationScripts(t *testing.T) { programArgs := ProgramArgs{ inputFile: file, - outputFile: subFolder + separator + mainFileName, + outputFile: subFolder + mainFileName, packageName: "main", // This appends a TestApp function to the output source so we @@ -112,17 +112,24 @@ func TestIntegrationScripts(t *testing.T) { // Run Go program. The "-v" option is important; without it most or // all of the fmt.* output would be suppressed. - testName := strings.Split(file, ".")[0][6:] - cmd = exec.Command( - "go", "test", + args := []string{ + "test", programArgs.outputFile, "-v", - "-race", - "-covermode=count", - "-coverprofile="+testName+".coverprofile", - "-coverpkg=./noarch,./linux,./darwin", - "--", "some", "args", - ) + } + if strings.Index(file, "examples/") == -1 { + testName := strings.Split(file, ".")[0][6:] + args = append( + args, + "-race", + "-covermode=count", + "-coverprofile="+testName+".coverprofile", + "-coverpkg=./noarch,./linux,./darwin", + ) + } + args = append(args, "--", "some", "args") + + cmd = exec.Command("go", args...) cmd.Stdin = strings.NewReader("7") cmd.Stdout = &goProgram.stdout cmd.Stderr = &goProgram.stderr @@ -206,7 +213,9 @@ func TestIntegrationScripts(t *testing.T) { // There is a separate check to see that both the C and Go programs // return the same exit code. removeLinesFromEnd := 5 - if strings.HasPrefix(goOutLines[len(goOutLines)-3], "exit status") { + if strings.Index(file, "examples/") >= 0 { + removeLinesFromEnd = 4 + } else if strings.HasPrefix(goOutLines[len(goOutLines)-3], "exit status") { removeLinesFromEnd = 3 }