github.com/hashicorp/packer@v1.14.3/builder/file/builder_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package file 5 6 import ( 7 "fmt" 8 "os" 9 "testing" 10 11 packersdk "github.com/hashicorp/packer-plugin-sdk/packer" 12 builderT "github.com/hashicorp/packer/acctest" 13 ) 14 15 func TestBuilder_implBuilder(t *testing.T) { 16 var _ packersdk.Builder = new(Builder) 17 } 18 19 func TestBuilderFileAcc_content(t *testing.T) { 20 builderT.Test(t, builderT.TestCase{ 21 Builder: &Builder{}, 22 Template: fileContentTest, 23 Check: checkContent, 24 }) 25 } 26 27 func TestBuilderFileAcc_copy(t *testing.T) { 28 builderT.Test(t, builderT.TestCase{ 29 Builder: &Builder{}, 30 Template: fileCopyTest, 31 Check: checkCopy, 32 }) 33 } 34 35 func checkContent(artifacts []packersdk.Artifact) error { 36 content, err := os.ReadFile("contentTest.txt") 37 if err != nil { 38 return err 39 } 40 contentString := string(content) 41 if contentString != "hello world!" { 42 return fmt.Errorf("Unexpected file contents: %s", contentString) 43 } 44 return nil 45 } 46 47 func checkCopy(artifacts []packersdk.Artifact) error { 48 content, err := os.ReadFile("copyTest.txt") 49 if err != nil { 50 return err 51 } 52 contentString := string(content) 53 if contentString != "Hello world.\n" { 54 return fmt.Errorf("Unexpected file contents: %s", contentString) 55 } 56 return nil 57 } 58 59 const fileContentTest = ` 60 { 61 "builders": [ 62 { 63 "type":"test", 64 "target":"contentTest.txt", 65 "content":"hello world!" 66 } 67 ] 68 } 69 ` 70 71 const fileCopyTest = ` 72 { 73 "builders": [ 74 { 75 "type":"test", 76 "target":"copyTest.txt", 77 "source":"test-fixtures/artifact.txt" 78 } 79 ] 80 } 81 `