From d469b5de232d9e99eb8112a091badeb0d1a9085a Mon Sep 17 00:00:00 2001 From: Dwi Siswanto Date: Sun, 25 Feb 2024 03:21:03 +0700 Subject: [PATCH] test(option): add case for non-existent & non-regular files Signed-off-by: Dwi Siswanto --- option/loader_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/option/loader_test.go b/option/loader_test.go index b89a8f3a..0c1b17ad 100644 --- a/option/loader_test.go +++ b/option/loader_test.go @@ -6,6 +6,7 @@ package option import ( "os" + "syscall" "testing" "github.com/kitabisa/teler-waf" @@ -254,6 +255,34 @@ func TestLoadFromJSONFile(t *testing.T) { assert.NotEqual(t, opt, teler.Options{}) } +func TestErrLoadFromJSONFile(t *testing.T) { + t.Run("nonexistent", func(t *testing.T) { + _, err := LoadFromJSONFile("nonexistent-file.json") + assert.NotNil(t, err) + }) + + t.Run("notregular", func(t *testing.T) { + tmpfile, err := os.CreateTemp("", "config*.json") + if err != nil { + t.Fatal(err) + } + + tmpfilePath := tmpfile.Name() + if err := os.Remove(tmpfilePath); err != nil { + t.Fatal(err) + } + + err = syscall.Mknod(tmpfilePath, syscall.S_IFCHR|0644, 0) + if err != nil { + t.Fatal(err) + } + defer os.Remove(tmpfilePath) + + _, err = LoadFromJSONFile(tmpfilePath) + assert.NotNil(t, err) + }) +} + func TestLoadFromYAMLBytes(t *testing.T) { opt, err := LoadFromYAMLBytes(yamlConfig) if err != nil { @@ -293,3 +322,31 @@ func TestLoadFromYAMLFile(t *testing.T) { assert.NotEqual(t, opt, teler.Options{}) } + +func TestErrLoadFromYAMLFile(t *testing.T) { + t.Run("nonexistent", func(t *testing.T) { + _, err := LoadFromYAMLFile("nonexistent-file.yaml") + assert.NotNil(t, err) + }) + + t.Run("notregular", func(t *testing.T) { + tmpfile, err := os.CreateTemp("", "config*.yaml") + if err != nil { + t.Fatal(err) + } + + tmpfilePath := tmpfile.Name() + if err := os.Remove(tmpfilePath); err != nil { + t.Fatal(err) + } + + err = syscall.Mknod(tmpfilePath, syscall.S_IFCHR|0644, 0) + if err != nil { + t.Fatal(err) + } + defer os.Remove(tmpfilePath) + + _, err = LoadFromYAMLFile(tmpfilePath) + assert.NotNil(t, err) + }) +}