Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed failing tests due to the change we check for file readability. #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

kberov
Copy link
Owner

@kberov kberov commented Sep 29, 2024

No description provided.

@HeikoSchlittermann
Copy link
Collaborator

I added another commit with a serving suggesting :) I strongly believe, you should not try to parse error strings. Use sentinel errors instead. Or return the errors you got from the os, if possible.

And, if you do fmt.Errorf, use %w to Wrap and not convert the errors.

@kberov
Copy link
Owner Author

kberov commented Sep 30, 2024

@HeikoSchlittermann, thanks for the suggestion. In tests now I use errors.Is(...) instead of parsing error strings.

gledki_test.go Outdated
Comment on lines 298 to 301
if errors.Is(err, os.ErrNotExist) {
t.Logf("Right error: %v", err)
} else {
t.Fatalf("Wrong error: errstr")
t.Fatalf("Wrong error: %v", err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I strongly suggest not to use t.Fatal*, unless you really want to abort the test. Better to use t.Error*.

  • t.Error* -> t.Log + t.Fail, which marks the testcase as failed, but continues
  • t.Fatal* -> t.Log + t.FailNow, which aborts the current test function

Same applies to the following test cases

@@ -331,21 +332,21 @@ func TestErrors(t *testing.T) {
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if syscall.ENOENT isn't a way to specific here, the more general os.ErrNotExist should be better suitable, IMHO. I'm not sure about portability of ENOENT (maybe other systems report an other errno, which hopefully will be translated by the Go libraries into os.ErrNotExist

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The system returns "no such file or directory". I found that this string comes from syscall.ENOENT and just used it as a sentinel error. os.ErrNotExist contains "file does not exist", which is a different string.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The syscall package is os dependend.

GOOS=windows go doc syscall
GOOS=linux go doc syscall

e.g. shows different things. Fortunately for the current situation, syscall.ENOENT seems to be available in both cases. But do we know about other OS?

I'd suggest, using the highest abstraction available, which is os.ErrNotExist, IMHO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants