Skip to content

Commit

Permalink
test(option): add case for non-existent & non-regular files
Browse files Browse the repository at this point in the history
Signed-off-by: Dwi Siswanto <me@dw1.io>
  • Loading branch information
dwisiswant0 committed Feb 24, 2024
1 parent 66778fd commit d469b5d
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions option/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package option

import (
"os"
"syscall"
"testing"

"github.com/kitabisa/teler-waf"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
})
}

0 comments on commit d469b5d

Please sign in to comment.