github.com/mponton/terratest@v0.44.0/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/mponton/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 := t.TempDir()
    47  	logFileName := fmt.Sprintf("./fixtures/%s_example.log", example)
    48  	expectedOutputDirName := fmt.Sprintf("./fixtures/%s_example_expected", example)
    49  	file := openFile(t, logFileName)
    50  	SpawnParsers(logger, file, dir)
    51  	assert.True(t, DirectoryEqual(t, dir, expectedOutputDirName))
    52  }
    53  
    54  func TestIntegrationBasicExample(t *testing.T) {
    55  	t.Parallel()
    56  	testExample(t, "basic")
    57  }
    58  
    59  func TestIntegrationFailingExample(t *testing.T) {
    60  	t.Parallel()
    61  	testExample(t, "failing")
    62  }
    63  
    64  func TestIntegrationPanicExample(t *testing.T) {
    65  	t.Parallel()
    66  	testExample(t, "panic")
    67  }
    68  
    69  func TestIntegrationNewGoExample(t *testing.T) {
    70  	t.Parallel()
    71  	testExample(t, "new_go_failing")
    72  }