-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
files: refactoring and improvements See merge request greut/eclint!7
- Loading branch information
Showing
10 changed files
with
373 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
"github.com/go-logr/logr" | ||
) | ||
|
||
// listFiles returns the list of files based on the input. | ||
// | ||
// When its empty, it relies on `git ls-files` first, which | ||
// whould fail if `git` is not present or the current working | ||
// directory is not managed by it. In that case, it work the | ||
// current working directory. | ||
// | ||
// When args are given, it recursively walks into them. | ||
func listFiles(log logr.Logger, args ...string) ([]string, error) { | ||
if len(args) == 0 { | ||
fs, err := gitLsFiles(log, ".") | ||
if err == nil { | ||
return fs, nil | ||
} | ||
|
||
log.Error(err, "git ls-files failure") | ||
args = append(args, ".") | ||
} | ||
|
||
return walk(log, args...) | ||
} | ||
|
||
// walk iterates on each path item recursively. | ||
func walk(log logr.Logger, paths ...string) ([]string, error) { | ||
files := make([]string, 0) | ||
for _, path := range paths { | ||
err := filepath.Walk(path, func(p string, i os.FileInfo, e error) error { | ||
if e != nil { | ||
return e | ||
} | ||
mode := i.Mode() | ||
if mode.IsRegular() && !mode.IsDir() { | ||
log.V(4).Info("index %s", p) | ||
files = append(files, p) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return files, err | ||
} | ||
} | ||
return files, nil | ||
} | ||
|
||
// gitLsFiles returns the list of file based on what is in the git index. | ||
// | ||
// -z is mandatory as some repositories non-ASCII file names which creates | ||
// quoted and escaped file names. | ||
func gitLsFiles(log logr.Logger, path string) ([]string, error) { | ||
output, err := exec.Command("git", "ls-files", "-z", path).Output() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fs := bytes.Split(output, []byte{0}) | ||
// last line is empty | ||
files := make([]string, len(fs)-1) | ||
for i := 0; i < len(files); i++ { | ||
p := string(fs[i]) | ||
log.V(4).Info("index %s", p) | ||
files[i] = p | ||
} | ||
return files, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
tlogr "github.com/go-logr/logr/testing" | ||
) | ||
|
||
const ( | ||
// testdataSimple contains a sample editorconfig directory with | ||
// some errors. | ||
testdataSimple = "testdata/simple" | ||
) | ||
|
||
func TestListFiles(t *testing.T) { | ||
l := tlogr.TestLogger{} | ||
d := testdataSimple | ||
fs, err := listFiles(l, d) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(fs) != 2 { | ||
t.Errorf("%s should have two files, got %d", d, len(fs)) | ||
} | ||
} | ||
|
||
func TestListFilesNoArgs(t *testing.T) { | ||
l := tlogr.TestLogger{} | ||
d := testdataSimple | ||
|
||
cwd, err := os.Getwd() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer func() { | ||
if err := os.Chdir(cwd); err != nil { | ||
t.Fatal(err) | ||
} | ||
}() | ||
|
||
err = os.Chdir(d) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
fs, err := listFiles(l) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(fs) != 2 { | ||
t.Errorf("%s should have two files, got %d", d, len(fs)) | ||
} | ||
} | ||
|
||
func TestListFilesNoGit(t *testing.T) { | ||
// FIXME... should be the null logger, right? | ||
l := tlogr.NullLogger{} | ||
d := fmt.Sprintf("/tmp/eclint/%d", os.Getpid()) | ||
|
||
err := os.MkdirAll(d, 0700) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
cwd, err := os.Getwd() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer func() { | ||
if err := os.Chdir(cwd); err != nil { | ||
t.Fatal(err) | ||
} | ||
}() | ||
err = os.Chdir(d) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
fs, err := listFiles(l) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(fs) != 0 { | ||
t.Errorf("%s should have two files, got %d", d, len(fs)) | ||
} | ||
} | ||
|
||
func TestWalk(t *testing.T) { | ||
l := tlogr.TestLogger{} | ||
d := testdataSimple | ||
fs, err := walk(l, d) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(fs) != 2 { | ||
t.Errorf("%s should have two files, got %d", d, len(fs)) | ||
} | ||
} | ||
|
||
func TestGitLsFiles(t *testing.T) { | ||
l := tlogr.TestLogger{} | ||
d := testdataSimple | ||
|
||
fs, err := gitLsFiles(l, d) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(fs) != 2 { | ||
t.Errorf("%s should have two files, got %d", d, len(fs)) | ||
} | ||
} | ||
|
||
func TestGitLsFilesFailure(t *testing.T) { | ||
l := tlogr.TestLogger{} | ||
d := fmt.Sprintf("/tmp/eclint/%d", os.Getpid()) | ||
|
||
err := os.MkdirAll(d, 0700) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
_, err = gitLsFiles(l, d) | ||
if err == nil { | ||
t.Error("an error was expected") | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.