github.com/hashicorp/packer@v1.14.3/acctest/testing_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package acctest
     5  
     6  import (
     7  	"os"
     8  	"testing"
     9  )
    10  
    11  func init() {
    12  	testTesting = true
    13  
    14  	if err := os.Setenv(TestEnvVar, "1"); err != nil {
    15  		panic(err)
    16  	}
    17  }
    18  
    19  func TestTest_noEnv(t *testing.T) {
    20  	// Unset the variable
    21  	t.Setenv(TestEnvVar, "")
    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  }