Skip to content

Commit

Permalink
[#258] Handle UnreachableDirectoryError in checker.checkLanguageFiles()
Browse files Browse the repository at this point in the history
- to display a warning per unreachable directory when running tcr check
  • Loading branch information
mengdaming committed Jul 28, 2023
1 parent 4357ba3 commit a8498b0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 19 deletions.
39 changes: 24 additions & 15 deletions src/checker/check_language.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package checker

import (
"github.com/murex/tcr/checker/model"
"github.com/murex/tcr/language"
"github.com/murex/tcr/params"
)

Expand Down Expand Up @@ -112,15 +113,7 @@ func checkLanguageSrcFiles(_ params.Params) (cp []model.CheckPoint) {
if checkEnv.lang == nil {
return cp
}
srcFiles, err := checkEnv.lang.AllSrcFiles()
if err != nil {
cp = append(cp, model.ErrorCheckPoint(err))
return cp
}
return model.CheckpointsForList(
"matching source files found:",
"no matching source file found",
srcFiles...)
return checkLanguageFiles("source", checkEnv.lang.AllSrcFiles)
}

func checkLanguageTestDirectories(_ params.Params) (cp []model.CheckPoint) {
Expand All @@ -147,15 +140,31 @@ func checkLanguageTestFiles(_ params.Params) (cp []model.CheckPoint) {
if checkEnv.lang == nil {
return cp
}
testFiles, err := checkEnv.lang.AllTestFiles()
if err != nil {
return checkLanguageFiles("test", checkEnv.lang.AllTestFiles)
}

func checkLanguageFiles(desc string, fileSearcher func() ([]string, error)) (cp []model.CheckPoint) {
languageFiles, err := fileSearcher()
switch err := err.(type) {
case nil:
// do nothing
case *language.UnreachableDirectoryError:
// unreachable directories: we display a warning for each, then continue
for _, dir := range err.DirList() {
cp = append(cp, model.WarningCheckPoint(
"cannot access "+desc+" directory ", dir))
}
default:
// unhandled errors
cp = append(cp, model.ErrorCheckPoint(err))
return cp
}
return model.CheckpointsForList(
"matching test files found:",
"no matching test file found",
testFiles...)

cp = append(cp, model.CheckpointsForList(
"matching "+desc+" files found:",
"no matching "+desc+" file found",
languageFiles...)...)
return cp
}

func languageAsText() string {
Expand Down
54 changes: 54 additions & 0 deletions src/checker/check_language_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,33 @@ func Test_check_language_src_files(t *testing.T) {
model.ErrorCheckPoint("some error"),
},
},
{
"unreachable directory error and no matching file", "xxx",
language.NewFakeLanguage("").WithAllSrcFiles(
func() ([]string, error) {
err := language.UnreachableDirectoryError{}
err.Add("dir1")
return nil, &err
}),
[]model.CheckPoint{
model.WarningCheckPoint("cannot access source directory dir1"),
model.WarningCheckPoint("no matching source file found"),
},
},
{
"unreachable directory error and one matching file", "xxx",
language.NewFakeLanguage("").WithAllSrcFiles(
func() ([]string, error) {
err := language.UnreachableDirectoryError{}
err.Add("dir1")
return []string{"src-file1"}, &err
}),
[]model.CheckPoint{
model.WarningCheckPoint("cannot access source directory dir1"),
model.OkCheckPoint("matching source files found:"),
model.OkCheckPoint("- src-file1"),
},
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
Expand Down Expand Up @@ -430,6 +457,33 @@ func Test_check_language_test_files(t *testing.T) {
model.ErrorCheckPoint("some error"),
},
},
{
"unreachable directory error and no matching file", "xxx",
language.NewFakeLanguage("").WithAllTestFiles(
func() ([]string, error) {
err := language.UnreachableDirectoryError{}
err.Add("dir1")
return nil, &err
}),
[]model.CheckPoint{
model.WarningCheckPoint("cannot access test directory dir1"),
model.WarningCheckPoint("no matching test file found"),
},
},
{
"unreachable directory error and one matching file", "xxx",
language.NewFakeLanguage("").WithAllTestFiles(
func() ([]string, error) {
err := language.UnreachableDirectoryError{}
err.Add("dir1")
return []string{"test-file1"}, &err
}),
[]model.CheckPoint{
model.WarningCheckPoint("cannot access test directory dir1"),
model.OkCheckPoint("matching test files found:"),
model.OkCheckPoint("- test-file1"),
},
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions src/language/file_tree_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (e *UnreachableDirectoryError) DirList() []string {
return e.dirs
}

// add adds directories to the list of unreachable directories
func (e *UnreachableDirectoryError) add(dir ...string) {
// Add adds directories to the list of unreachable directories
func (e *UnreachableDirectoryError) Add(dir ...string) {
e.dirs = append(e.dirs, dir...)
}

Expand Down Expand Up @@ -123,7 +123,7 @@ func (ftf FileTreeFilter) findAllMatchingFiles(baseDir string) (files []string,
for _, dir := range ftf.Directories {
matchingFiles, err := ftf.findMatchingFilesInDir(baseDir, dir)
if err != nil {
dirErr.add(filepath.Join(baseDir, dir))
dirErr.Add(filepath.Join(baseDir, dir))
continue
}
files = append(files, matchingFiles...)
Expand Down
2 changes: 1 addition & 1 deletion src/language/file_tree_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func Test_unreachable_directory_error(t *testing.T) {
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
e := UnreachableDirectoryError{}
e.add(test.dirs...)
e.Add(test.dirs...)
assert.Equal(t, test.dirs, e.DirList())
assert.Equal(t, test.expected, e.Error())
})
Expand Down

0 comments on commit a8498b0

Please sign in to comment.