github.com/HashDataInc/packer@v1.3.2/helper/builder/testing/testing_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  )
     7  
     8  func init() {
     9  	testTesting = true
    10  
    11  	if err := os.Setenv(TestEnvVar, "1"); err != nil {
    12  		panic(err)
    13  	}
    14  }
    15  
    16  func TestTest_noEnv(t *testing.T) {
    17  	// Unset the variable
    18  	if err := os.Setenv(TestEnvVar, ""); err != nil {
    19  		t.Fatalf("err: %s", err)
    20  	}
    21  	defer os.Setenv(TestEnvVar, "1")
    22  
    23  	mt := new(mockT)
    24  	Test(mt, TestCase{})
    25  
    26  	if !mt.SkipCalled {
    27  		t.Fatal("skip not called")
    28  	}
    29  }
    30  
    31  func TestTest_preCheck(t *testing.T) {
    32  	called := false
    33  
    34  	mt := new(mockT)
    35  	Test(mt, TestCase{
    36  		PreCheck: func() { called = true },
    37  	})
    38  
    39  	if !called {
    40  		t.Fatal("precheck should be called")
    41  	}
    42  }
    43  
    44  // mockT implements TestT for testing
    45  type mockT struct {
    46  	ErrorCalled bool
    47  	ErrorArgs   []interface{}
    48  	FatalCalled bool
    49  	FatalArgs   []interface{}
    50  	SkipCalled  bool
    51  	SkipArgs    []interface{}
    52  
    53  	f bool
    54  }
    55  
    56  func (t *mockT) Error(args ...interface{}) {
    57  	t.ErrorCalled = true
    58  	t.ErrorArgs = args
    59  	t.f = true
    60  }
    61  
    62  func (t *mockT) Fatal(args ...interface{}) {
    63  	t.FatalCalled = true
    64  	t.FatalArgs = args
    65  	t.f = true
    66  }
    67  
    68  func (t *mockT) Skip(args ...interface{}) {
    69  	t.SkipCalled = true
    70  	t.SkipArgs = args
    71  	t.f = true
    72  }
    73  
    74  func (t *mockT) failed() bool {
    75  	return t.f
    76  }
    77  
    78  func (t *mockT) failMessage() string {
    79  	if t.FatalCalled {
    80  		return t.FatalArgs[0].(string)
    81  	} else if t.ErrorCalled {
    82  		return t.ErrorArgs[0].(string)
    83  	} else if t.SkipCalled {
    84  		return t.SkipArgs[0].(string)
    85  	}
    86  
    87  	return "unknown"
    88  }