diff --git a/examples_test.go b/examples_test.go index 6fb19ab..ab6878a 100644 --- a/examples_test.go +++ b/examples_test.go @@ -45,7 +45,7 @@ func Example_New_err() { fmt.Println(err.Error()) } // Output: - // Gledki root directory '/ala/bala' does not exist! + // Gledki root directory '/ala/bala': file does not exist. } func ExampleGledki_Execute_simple() { diff --git a/gledki.go b/gledki.go index 32fcaee..161d39a 100644 --- a/gledki.go +++ b/gledki.go @@ -330,7 +330,7 @@ func (t *Gledki) findRoots(roots []string) error { t.Roots = append(t.Roots, byCwd) continue } else { - return fmt.Errorf("gledki root directory '%s' does not exist! You have to create it. ", byCwd) + return fmt.Errorf("Gledki root directory '%s': %w! You have to create it.", byCwd, os.ErrNotExist) } } @@ -338,7 +338,7 @@ func (t *Gledki) findRoots(roots []string) error { t.Roots = append(t.Roots, root) continue } else { - return fmt.Errorf("Gledki root directory '%s' does not exist!", root) + return fmt.Errorf("Gledki root directory '%s': %w.", root, os.ErrNotExist) } } return nil diff --git a/gledki_test.go b/gledki_test.go index 1f6ee98..8a0081c 100644 --- a/gledki_test.go +++ b/gledki_test.go @@ -2,12 +2,14 @@ package gledki import ( "bytes" + "errors" "fmt" "io" "io/fs" "os" "path/filepath" "strings" + "syscall" "testing" "github.com/labstack/gommon/log" @@ -41,7 +43,7 @@ func TestNew(t *testing.T) { // load templates tpls, err := New(includePaths, filesExt, tagsPair, true) if err != nil { - t.Fatal("Error New: ", err.Error()) + t.Error("Error New: ", err.Error()) } else { tpls.Logger = logger t.Logf("\ngledki.New loads all files in %s", includePaths) @@ -54,10 +56,10 @@ func TestNew(t *testing.T) { tpls, err = New(includePaths, filesExt, tagsPair, false) tpls.Logger = logger if err != nil { - t.Fatal("Eror New: ", err.Error()) + t.Error("Eror New: ", err.Error()) } if len(tpls.files) > 0 { - t.Fatal("templates should not be loaded") + t.Error("templates should not be loaded") } //Try to load nonreadable templates os.Chmod(includePaths[0]+"/../tpls_bad/_noread.htm", 0300) @@ -66,7 +68,7 @@ func TestNew(t *testing.T) { t.Logf("Expected error from New: %s", err.Error()) os.Chmod(includePaths[0]+"/../tpls_bad/_noread.htm", 0400) } else { - t.Fatal("Reading nonreadable file should have failed!") + t.Error("Reading nonreadable file should have failed!") } } @@ -88,7 +90,7 @@ func TestExecute(t *testing.T) { t.Log(outstr) for k, v := range data { if !strings.Contains(outstr, v.(string)) { - t.Fatalf("output does not contain expected value for '%s': %s", k, v) + t.Errorf("output does not contain expected value for '%s': %s", k, v) } } @@ -108,7 +110,7 @@ func TestExecute(t *testing.T) { t.Log(outstr) for k, v := range tpls.Stash { if !strings.Contains(outstr, v.(string)) { - t.Fatalf("output does not contain expected value for '%s': %s", k, v) + t.Errorf("output does not contain expected value for '%s': %s", k, v) } } @@ -120,7 +122,7 @@ func TestExecute(t *testing.T) { t.Log(outstr) for k, v := range tpls.Stash { if !strings.Contains(outstr, v.(string)) { - t.Fatalf("output does not contain expected value for '%s': %s", k, v) + t.Errorf("output does not contain expected value for '%s': %s", k, v) } } } @@ -172,12 +174,12 @@ func TestAddExecuteFunc(t *testing.T) { // Even later, when the whole page is put together _, err := tpls.Execute(&out, "book") if err != nil { - t.Fatalf("Error executing Gledki.Execute: %s", err.Error()) + t.Errorf("Error executing Gledki.Execute: %s", err.Error()) } if strings.Contains(out.String(), `
`) { t.Log("Expected content") } else { - t.Fatalf("Expected content was not found:\n%s", out.String()) + t.Errorf("Expected content was not found:\n%s", out.String()) } } @@ -208,18 +210,18 @@ func TestAddExecuteFuncWithTheme(t *testing.T) { // Even later, when the whole page is put together _, err := tpls.Execute(&out, "book") if err != nil { - t.Fatalf("Error executing Gledki.Execute: %s", err.Error()) + t.Errorf("Error executing Gledki.Execute: %s", err.Error()) } outStr := out.String() if strings.Contains(outStr, `
`) { t.Log("Expected 'black' class") } else { - t.Fatalf("Expected class 'black' was not found:\n%s", outStr) + t.Errorf("Expected class 'black' was not found:\n%s", outStr) } if strings.Contains(outStr, `black`) { t.Log("Expected 'black' title") } else { - t.Fatalf("Expected 'black' title was not found:\n%s", outStr) + t.Errorf("Expected 'black' title was not found:\n%s", outStr) } // t.Log(outStr) @@ -271,13 +273,13 @@ func TestIncludeLimitNoPanic(t *testing.T) { out.Reset() _, err := tpls.Execute(&out, "includes") if err != nil { - t.Fatalf("Error executing Gledki.Execute: %s", err.Error()) + t.Errorf("Error executing Gledki.Execute: %s", err.Error()) } outstr := out.String() t.Log(outstr) if !strings.Contains(outstr, "4 4") { - t.Fatalf("output does not contain expected value 4 4") + t.Errorf("output does not contain expected value 4 4") } } @@ -286,86 +288,85 @@ func TestFtExecString(t *testing.T) { partial := `<div class="pager">${prev}${next}</div>` out := tpls.FtExecString(partial, Stash{`prev`: `previous`}) if strings.Contains(out, "next") { - t.Fatal("String should not contain unused placeholder 'next'!") + t.Error("String should not contain unused placeholder 'next'!") } } func TestErrors(t *testing.T) { if _, err := New([]string{"/ala/bala/nica"}, filesExt, tagsPair, false); err != nil { - errstr := err.Error() - if strings.Contains(errstr, "does not exist") { - t.Logf("Right error: %s", err.Error()) + if errors.Is(err, os.ErrNotExist) { + t.Logf("Right error: %v", err) } else { - t.Fatalf("Wrong error: errstr") + t.Errorf("Wrong error: %v", err) } } else { - t.Fatal("No error - this is unexpected!") + t.Error("No error - this is unexpected!") } tpls, _ := New([]string{includePaths[0] + "/../tpls_bad"}, filesExt, tagsPair, false) tpls.Logger = logger out.Reset() if _, err := tpls.Execute(&out, "no_wrapper"); err != nil { errstr := err.Error() - if strings.Contains(errstr, "could not be read") { - t.Logf("Right error: %s", err.Error()) + if errors.Is(err, syscall.ENOENT) { + t.Logf("Right error: %s", errstr) } else { - t.Fatalf("Wrong error: errstr") + t.Errorf(`Wrong error: %s`, errstr) } } else { - t.Fatal("No error - this is unexpected!") + t.Error("No error - this is unexpected!") } out.Reset() if _, err := tpls.Execute(&out, "nosuchfile"); err != nil { errstr := err.Error() - if strings.Contains(errstr, "could not be read") { - t.Logf("Right error: %s", err.Error()) + if errors.Is(err, syscall.ENOENT) { + t.Logf("Right error: %s", errstr) } else { - t.Fatalf("Wrong error: errstr") + t.Errorf("Wrong error: %s", errstr) } } else { - t.Fatal("No error - this is unexpected!") + t.Error("No error - this is unexpected!") } out.Reset() if _, err := tpls.Execute(&out, "no_include"); err != nil { errstr := err.Error() - if strings.Contains(errstr, "could not be read") { - t.Logf("Right error: %s", err.Error()) + if errors.Is(err, syscall.ENOENT) { + t.Logf("Right error: %s", errstr) } else { - t.Fatalf("Wrong error:%s", errstr) + t.Errorf("Wrong error: %s", errstr) } } else { - t.Fatalf("No error - this is unexpected! Output: %s", out.String()) + t.Errorf("No error - this is unexpected! Output: %s", out.String()) } out.Reset() if _, err := tpls.Execute(&out, "incl_no_wrapper.htm"); err != nil { errstr := err.Error() - if strings.Contains(errstr, "could not be read") { - t.Logf("Right error: %s", err.Error()) + if errors.Is(err, syscall.ENOENT) { + t.Logf("Right error: %s", errstr) } else { - t.Fatalf("Wrong error:%s", errstr) + t.Errorf("Wrong error: %s", errstr) } } else { - t.Fatalf("No error - this is unexpected! Output: %s", out.String()) + t.Errorf("No error - this is unexpected! Output: %s", out.String()) } out.Reset() if _, err := tpls.Execute(&out, "incl_no_include.htm"); err != nil { errstr := err.Error() - if strings.Contains(errstr, "could not be read") { - t.Logf("Right error: %s", err.Error()) + if errors.Is(err, syscall.ENOENT) { + t.Logf("Right error: %s", errstr) } else { - t.Fatalf("Wrong error:%s", errstr) + t.Errorf("Wrong error:%s", errstr) } } else { - t.Fatalf("No error - this is unexpected! Output: %s", out.String()) + t.Errorf("No error - this is unexpected! Output: %s", out.String()) } absRoot, err := filepath.Abs(includePaths[0]) if err != nil { - t.Fatalf("Error finding absolute path: %s", err.Error()) + t.Errorf("Error finding absolute path: %s", err.Error()) } _ = tpls.findRoots([]string{absRoot}) if tpls.Roots[0] == absRoot { @@ -376,10 +377,10 @@ func TestErrors(t *testing.T) { if err = tpls.findRoots([]string{"../ala/bala"}); err != nil { errstr := err.Error() - if strings.Contains(errstr, "does not exist!") { - t.Logf("Right error: %s", err.Error()) + if errors.Is(err, os.ErrNotExist) { + t.Logf("Right error: %s", errstr) } else { - t.Fatalf("Wrong error:%s", errstr) + t.Errorf("Wrong error:%s", errstr) } } } @@ -387,7 +388,7 @@ func TestErrors(t *testing.T) { func expectPanic(t *testing.T, f func()) { defer func() { if r := recover(); r == nil { - t.Fatalf("MISSING PANIC") + t.Error("MISSING PANIC") } else { t.Log(r) }