github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/modules/logger/parser/integration_test.go (about) 1 package parser 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/gruntwork-io/terratest/modules/shell" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func DirectoryEqual(t *testing.T, dirA string, dirB string) bool { 14 dirAAbs, err := filepath.Abs(dirA) 15 if err != nil { 16 t.Fatal(err) 17 } 18 dirBAbs, err := filepath.Abs(dirB) 19 if err != nil { 20 t.Fatal(err) 21 } 22 // We use diff here instead of using something in go for simplicity of comparing directories and file contents 23 // recursively 24 cmd := shell.Command{ 25 Command: "diff", 26 Args: []string{"-ar", dirAAbs, dirBAbs}, 27 } 28 err = shell.RunCommandE(t, cmd) 29 exitCode, err := shell.GetExitCodeForRunCommandError(err) 30 if err != nil { 31 t.Fatal(err) 32 } 33 return exitCode == 0 34 } 35 36 func openFile(t *testing.T, filename string) *os.File { 37 file, err := os.Open(filename) 38 if err != nil { 39 t.Fatalf("Error opening file: %s", err) 40 } 41 return file 42 } 43 44 func testExample(t *testing.T, example string) { 45 logger := NewTestLogger(t) 46 dir := getTempDir(t) 47 defer os.RemoveAll(dir) 48 logFileName := fmt.Sprintf("./fixtures/%s_example.log", example) 49 expectedOutputDirName := fmt.Sprintf("./fixtures/%s_example_expected", example) 50 file := openFile(t, logFileName) 51 SpawnParsers(logger, file, dir) 52 assert.True(t, DirectoryEqual(t, dir, expectedOutputDirName)) 53 } 54 55 func TestIntegrationBasicExample(t *testing.T) { 56 t.Parallel() 57 testExample(t, "basic") 58 } 59 60 func TestIntegrationFailingExample(t *testing.T) { 61 t.Parallel() 62 testExample(t, "failing") 63 } 64 65 func TestIntegrationPanicExample(t *testing.T) { 66 t.Parallel() 67 testExample(t, "panic") 68 } 69 70 func TestIntegrationNewGoExample(t *testing.T) { 71 t.Parallel() 72 testExample(t, "new_go_failing") 73 }