From 4ac9ef850baaee0838b29001cac5a6798d3c08a7 Mon Sep 17 00:00:00 2001 From: Ian Lewis Date: Sat, 19 Aug 2023 11:39:38 +0900 Subject: [PATCH] chore: enable nestif linter (#12) Signed-off-by: Ian Lewis --- .golangci.yml | 3 +- lexer_test.go | 88 +++++++++++++++++++----------------- parser_test.go | 87 +++++++++++++++++------------------ repository_test.go | 48 +++++++++++--------- util_test.go | 110 +++++++++++++++++++++++---------------------- 5 files changed, 175 insertions(+), 161 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index eef4460..d648d06 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -79,8 +79,7 @@ linters: # TODO(#5): enable misspell linter # - misspell - nakedret - # TODO(#5): enable nestif linter - # - nestif + - nestif - nolintlint # TODO(#5): enable paralleltest linter # - paralleltest diff --git a/lexer_test.go b/lexer_test.go index 6b5e1db..212eb50 100644 --- a/lexer_test.go +++ b/lexer_test.go @@ -115,58 +115,62 @@ func lexer(t *testing.T, lines []string, eol string, tokens []token, e error) { // did we receive a token? if _got == nil { t.Fatalf("expected token at %s; none found", _lexer) - } else if _got.Type != _expected.Type { + } + + if _got.Type != _expected.Type { t.Fatalf( "token type mismatch; expected type %d, got %d [%s]", _expected.Type, _got.Type, _got, ) - } else if _got.Name() != _expected.Name { + } + + if _got.Name() != _expected.Name { t.Fatalf( "token name mismatch; expected name %q, got %q [%s]", _expected.Name, _got.Name(), _got, ) - } else { - // ensure the extracted token string matches expectation - // - we handle EOL separately, since it can change based - // on the end of line sequence of the input file - _same := _got.Token() == _expected.Token - if _got.Type == gitignore.EOL { - _same = _got.Token() == eol - } - if !_same { - t.Fatalf( - "token value mismatch; expected name %q, got %q [%s]", - _expected.Token, _got.Token(), _got, - ) - } + } - // ensure the token position matches the original lexer position - if !coincident(_got.Position, _position) { - t.Fatalf( - "token position mismatch for %s; expected %s, got %s", - _got, pos(_position), pos(_got.Position), - ) - } + // ensure the extracted token string matches expectation + // - we handle EOL separately, since it can change based + // on the end of line sequence of the input file + _same := _got.Token() == _expected.Token + if _got.Type == gitignore.EOL { + _same = _got.Token() == eol + } + if !_same { + t.Fatalf( + "token value mismatch; expected name %q, got %q [%s]", + _expected.Token, _got.Token(), _got, + ) + } - // ensure the token position matches the expected position - // - since we will be testing with different line endings, we - // have to choose the correct offset - _position := gitignore.Position{ - File: "", - Line: _expected.Line, - Column: _expected.Column, - Offset: _expected.NewLine, - } - if eol == "\r\n" { - _position.Offset = _expected.CarriageReturn - } - if !coincident(_got.Position, _position) { - t.Log(pos(_got.Position) + "\t" + _got.String()) - t.Fatalf( - "token position mismatch; expected %s, got %s", - pos(_position), pos(_got.Position), - ) - } + // ensure the token position matches the original lexer position + if !coincident(_got.Position, _position) { + t.Fatalf( + "token position mismatch for %s; expected %s, got %s", + _got, pos(_position), pos(_got.Position), + ) + } + + // ensure the token position matches the expected position + // - since we will be testing with different line endings, we + // have to choose the correct offset + _ignorePosition := gitignore.Position{ + File: "", + Line: _expected.Line, + Column: _expected.Column, + Offset: _expected.NewLine, + } + if eol == "\r\n" { + _ignorePosition.Offset = _expected.CarriageReturn + } + if !coincident(_got.Position, _ignorePosition) { + t.Log(pos(_got.Position) + "\t" + _got.String()) + t.Fatalf( + "token position mismatch; expected %s, got %s", + pos(_ignorePosition), pos(_got.Position), + ) } } diff --git a/parser_test.go b/parser_test.go index c266754..d3bd356 100644 --- a/parser_test.go +++ b/parser_test.go @@ -147,54 +147,55 @@ func parse(t *testing.T, test *parsetest) { "parse pattern mismatch; expected %d patterns, got %d", test.good, len(_patterns), ) - } else { - // ensure the good pattern positions are correct - for _i := 0; _i < len(_patterns); _i++ { - _got := _patterns[_i].Position() - _expected := _GITPOSITION[_i] + return + } - if !coincident(_got, _expected) { - t.Errorf( - "pattern position mismatch; expected %q, got %q", - pos(_expected), pos(_got), - ) - } + // ensure the good pattern positions are correct + for _i := 0; _i < len(_patterns); _i++ { + _got := _patterns[_i].Position() + _expected := _GITPOSITION[_i] + + if !coincident(_got, _expected) { + t.Errorf( + "pattern position mismatch; expected %q, got %q", + pos(_expected), pos(_got), + ) } + } - // ensure the retrieved patterns are correct - // - we check the string form of the pattern against the respective - // lines from the .gitignore - // - we must special-case patterns that end in whitespace that - // can be ignored (i.e. it's not escaped) - _lines := strings.Split(_GITIGNORE, "\n") - for _i := 0; _i < len(_patterns); _i++ { - _pattern := _patterns[_i] - _got := _pattern.String() - _line := _pattern.Position().Line - _expected := _lines[_line-1] - - if _got != _expected { - // if the two strings aren't the same, then check to see if - // the difference is trailing whitespace - // - the expected string may have whitespace, while the - // pattern string does not - // - patterns have their trailing whitespace removed, so - // - we perform this check here, since it's possible for - // a pattern to end in a whitespace character (e.g. '\ ') - // and we don't want to be too heavy handed with our - // removal of whitespace - // - only do this check for non-comments - if !strings.HasPrefix(_expected, "#") { - _new := strings.TrimRight(_expected, " \t") - if _new == _got { - continue - } + // ensure the retrieved patterns are correct + // - we check the string form of the pattern against the respective + // lines from the .gitignore + // - we must special-case patterns that end in whitespace that + // can be ignored (i.e. it's not escaped) + _lines := strings.Split(_GITIGNORE, "\n") + for _i := 0; _i < len(_patterns); _i++ { + _pattern := _patterns[_i] + _got := _pattern.String() + _line := _pattern.Position().Line + _expected := _lines[_line-1] + + if _got != _expected { + // if the two strings aren't the same, then check to see if + // the difference is trailing whitespace + // - the expected string may have whitespace, while the + // pattern string does not + // - patterns have their trailing whitespace removed, so + // - we perform this check here, since it's possible for + // a pattern to end in a whitespace character (e.g. '\ ') + // and we don't want to be too heavy handed with our + // removal of whitespace + // - only do this check for non-comments + if !strings.HasPrefix(_expected, "#") { + _new := strings.TrimRight(_expected, " \t") + if _new == _got { + continue } - t.Errorf( - "pattern mismatch; expected %q, got %q at %s", - _expected, _got, pos(_pattern.Position()), - ) } + t.Errorf( + "pattern mismatch; expected %q, got %q at %s", + _expected, _got, pos(_pattern.Position()), + ) } } } // parse() diff --git a/repository_test.go b/repository_test.go index 7a37359..5e3cb79 100644 --- a/repository_test.go +++ b/repository_test.go @@ -36,6 +36,31 @@ type repositorytest struct { gitdir string } // repostorytest{} +func (r *repositorytest) setGitDir(gitdir bool) error { + // should we create the global exclude file + r.gitdir = os.Getenv("GIT_DIR") + if gitdir { + // create a temporary file for the global exclude file + _exclude, _err := exclude(_GITEXCLUDE) + if _err != nil { + return _err + } + + // extract the current value of the GIT_DIR environment variable + // and set the value to be that of the temporary file + r.exclude = _exclude + if _err = os.Setenv("GIT_DIR", r.exclude); _err != nil { + return _err + } + } else { + if _err := os.Unsetenv("GIT_DIR"); _err != nil { + return _err + } + } + + return nil +} + func (r *repositorytest) create(path string, gitdir bool) (gitignore.GitIgnore, error) { // if we have an error handler, reset the list of errors if r.error != nil { @@ -43,27 +68,8 @@ func (r *repositorytest) create(path string, gitdir bool) (gitignore.GitIgnore, } if r.file == gitignore.File || r.file == "" { - // should we create the global exclude file - r.gitdir = os.Getenv("GIT_DIR") - if gitdir { - // create a temporary file for the global exclude file - _exclude, _err := exclude(_GITEXCLUDE) - if _err != nil { - return nil, _err - } - - // extract the current value of the GIT_DIR environment variable - // and set the value to be that of the temporary file - r.exclude = _exclude - _err = os.Setenv("GIT_DIR", r.exclude) - if _err != nil { - return nil, _err - } - } else { - _err := os.Unsetenv("GIT_DIR") - if _err != nil { - return nil, _err - } + if err := r.setGitDir(gitdir); err != nil { + return nil, err } } diff --git a/util_test.go b/util_test.go index 2a40f6d..42df268 100644 --- a/util_test.go +++ b/util_test.go @@ -66,63 +66,67 @@ func dir(content map[string]string) (string, error) { return "", _err } + // Return early if there is no content. + if content == nil { + // return the temporary directory name + return _dir, nil + } + // populate the temporary directory with the content map // - each key of the map is a file name // - each value of the map is the file content // - file names are relative to the temporary directory - if content != nil { - for _key, _content := range content { - // ensure we have content to store - if _content == "" { - continue - } - - // should we create a directory or a file? - _isdir := false - _path := _key - if strings.HasSuffix(_path, "/") { - _path = strings.TrimSuffix(_path, "/") - _isdir = true - } - - // construct the absolute path (according to the local file system) - _abs := _dir - _parts := strings.Split(_path, "/") - _last := len(_parts) - 1 - if _isdir { - _abs = filepath.Join(_abs, filepath.Join(_parts...)) - } else if _last > 0 { - _abs = filepath.Join(_abs, filepath.Join(_parts[:_last]...)) - } - - // ensure this directory exists - _err = os.MkdirAll(_abs, _GITMASK) - if _err != nil { - defer os.RemoveAll(_dir) - return "", _err - } else if _isdir { - continue - } - - // create the absolute path for the target file - _abs = filepath.Join(_abs, _parts[_last]) - - // write the contents to this file - _file, _err := os.Create(_abs) - if _err != nil { - defer os.RemoveAll(_dir) - return "", _err - } - _, _err = _file.WriteString(_content) - if _err != nil { - defer os.RemoveAll(_dir) - return "", _err - } - _err = _file.Close() - if _err != nil { - defer os.RemoveAll(_dir) - return "", _err - } + for _key, _content := range content { + // ensure we have content to store + if _content == "" { + continue + } + + // should we create a directory or a file? + _isdir := false + _path := _key + if strings.HasSuffix(_path, "/") { + _path = strings.TrimSuffix(_path, "/") + _isdir = true + } + + // construct the absolute path (according to the local file system) + _abs := _dir + _parts := strings.Split(_path, "/") + _last := len(_parts) - 1 + if _isdir { + _abs = filepath.Join(_abs, filepath.Join(_parts...)) + } else if _last > 0 { + _abs = filepath.Join(_abs, filepath.Join(_parts[:_last]...)) + } + + // ensure this directory exists + _err = os.MkdirAll(_abs, _GITMASK) + if _err != nil { + defer os.RemoveAll(_dir) + return "", _err + } else if _isdir { + continue + } + + // create the absolute path for the target file + _abs = filepath.Join(_abs, _parts[_last]) + + // write the contents to this file + _file, _err := os.Create(_abs) + if _err != nil { + defer os.RemoveAll(_dir) + return "", _err + } + _, _err = _file.WriteString(_content) + if _err != nil { + defer os.RemoveAll(_dir) + return "", _err + } + _err = _file.Close() + if _err != nil { + defer os.RemoveAll(_dir) + return "", _err } }