Skip to content

Commit

Permalink
chore: enable nestif linter (#12)
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Lewis <[email protected]>
  • Loading branch information
Ian Lewis authored Aug 19, 2023
1 parent 77a4462 commit 4ac9ef8
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 161 deletions.
3 changes: 1 addition & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
88 changes: 46 additions & 42 deletions lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
}
}

Expand Down
87 changes: 44 additions & 43 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
48 changes: 27 additions & 21 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,40 @@ 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 {
r.errors = make([]gitignore.Error, 0)
}

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
}
}

Expand Down
110 changes: 57 additions & 53 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit 4ac9ef8

Please sign in to comment.