From 6a50f783b59bf58dac8fe07fe19b8f16e3a9fd60 Mon Sep 17 00:00:00 2001 From: Katy Moe Date: Wed, 15 May 2019 13:28:22 +0100 Subject: [PATCH] add test for empty file content case --- local/resource_local_file_test.go | 52 +++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/local/resource_local_file_test.go b/local/resource_local_file_test.go index 43fba769..54e1d3c4 100644 --- a/local/resource_local_file_test.go +++ b/local/resource_local_file_test.go @@ -152,5 +152,57 @@ resource "local_file" "file" { }) defer os.Remove(destinationDirPath) +} + +func TestLocalFile_EmptyContent(t *testing.T) { + var cases = []struct { + path string + content string + config string + }{ + { + "local_file", + "This is some content", + `resource "local_file" "file" { + content = "This is some content" + filename = "local_file" + }`, + }, + { + "local_file", + "", + `resource "local_file" "file" { + content = "" + filename = "local_file" + }`, + }, + } + for _, tt := range cases { + r.UnitTest(t, r.TestCase{ + Providers: testProviders, + Steps: []r.TestStep{ + { + Config: tt.config, + Check: func(s *terraform.State) error { + content, err := ioutil.ReadFile(tt.path) + if err != nil { + return fmt.Errorf("config:\n%s\n,got: %s\n", tt.config, err) + } + if string(content) != tt.content { + return fmt.Errorf("config:\n%s\ngot:\n%s\nwant:\n%s\n", tt.config, content, tt.content) + } + return nil + }, + Destroy: false, + }, + }, + CheckDestroy: func(*terraform.State) error { + if _, err := os.Stat(tt.path); os.IsNotExist(err) { + return nil + } + return errors.New("local_file did not get destroyed") + }, + }) + } }