Skip to content

Commit

Permalink
Merge pull request #69 from boyter/issue61
Browse files Browse the repository at this point in the history
Add support to run over multiple files or directories
  • Loading branch information
dbaggerman authored Apr 28, 2019
2 parents 85e1a89 + 7013d6e commit d796740
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
2 changes: 1 addition & 1 deletion processor/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func walkDirectoryParallel(root string, output chan *FileJob) {
}

wg.Wait()
close(output)

if Debug {
printDebug(fmt.Sprintf("milliseconds to walk directory: %d", makeTimestampMilli()-startTime))
}
Expand Down
3 changes: 3 additions & 0 deletions processor/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func TestWalkDirectoryParallel(t *testing.T) {

inputChan := make(chan *FileJob, 10000)
walkDirectoryParallel("../", inputChan)
close(inputChan)

count := 0
for range inputChan {
Expand All @@ -108,6 +109,7 @@ func TestWalkDirectoryParallelWorksWithSingleInputFile(t *testing.T) {

inputChan := make(chan *FileJob, 10000)
walkDirectoryParallel("file_test.go", inputChan)
close(inputChan)

count := 0
for range inputChan {
Expand All @@ -133,6 +135,7 @@ func TestWalkDirectoryParallelIgnoresRootTrailingSlash(t *testing.T) {

inputChan := make(chan *FileJob, 10000)
walkDirectoryParallel("file_test.go/", inputChan)
close(inputChan)

count := 0
for range inputChan {
Expand Down
27 changes: 22 additions & 5 deletions processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,15 @@ func Process() {
if len(DirFilePaths) == 0 {
DirFilePaths = append(DirFilePaths, ".")
}
fpath := filepath.Clean(DirFilePaths[0])

if _, err := os.Stat(fpath); os.IsNotExist(err) {
fmt.Println("file or directory does not exist: " + fpath)
return
// Check if the paths or files added exist and exit if not
for _, f := range DirFilePaths {
fpath := filepath.Clean(f)

if _, err := os.Stat(fpath); os.IsNotExist(err) {
fmt.Println("file or directory does not exist: " + fpath)
os.Exit(1)
}
}

SortBy = strings.ToLower(SortBy)
Expand All @@ -330,7 +334,20 @@ func Process() {
fileReadContentJobQueue := make(chan *FileJob, FileReadContentJobQueueSize) // Files ready to be processed
fileSummaryJobQueue := make(chan *FileJob, FileSummaryJobQueueSize) // Files ready to be summarised

go walkDirectoryParallel(fpath, fileListQueue)
go func() {
var wg sync.WaitGroup
for _, f := range DirFilePaths {
wg.Add(1)
fpath := filepath.Clean(f)

go func() {
walkDirectoryParallel(fpath, fileListQueue)
wg.Done()
}()
}
wg.Wait()
close(fileListQueue)
}()
go fileReaderWorker(fileListQueue, fileReadContentJobQueue)
go fileProcessorWorker(fileReadContentJobQueue, fileSummaryJobQueue)

Expand Down
43 changes: 40 additions & 3 deletions test-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ else
echo -e "${GREEN}PASSED invalid option test"
fi

if ./scc NOTAREALDIRECTORYORFILE > /dev/null ; then
echo -e "${RED}================================================="
echo -e "FAILED Invalid file/directory should produce error code "
echo -e "======================================================="
exit
else
echo -e "${GREEN}PASSED invalid file/directory test"
fi

if ./scc > /dev/null ; then
echo -e "${GREEN}PASSED no directory specified test"
else
Expand Down Expand Up @@ -114,6 +123,35 @@ else
exit
fi

# Multiple directory or file arguments
if ./scc main.go README.md | grep -q "Go " ; then
echo -e "${GREEN}PASSED multiple file argument test 1"
else
echo -e "${RED}======================================================="
echo -e "FAILED Should work with multiple file arguments 1"
echo -e "======================================================="
exit
fi

if ./scc main.go README.md | grep -q "Markdown " ; then
echo -e "${GREEN}PASSED multiple file argument test 2"
else
echo -e "${RED}======================================================="
echo -e "FAILED Should work with multiple file arguments 2"
echo -e "======================================================="
exit
fi

if ./scc processor scripts > /dev/null ; then
echo -e "${GREEN}PASSED multiple directory specified test"
else
echo -e "${RED}======================================================="
echo -e "FAILED Should run correctly with multiple directory specified"
echo -e "================================================="
exit
fi


# Try out duplicates
for i in {1..100}
do
Expand All @@ -128,14 +166,13 @@ do
done
echo -e "${GREEN}PASSED duplicates test"


# Try out specific languages
# TODO make this a loop with every example
if ./scc "examples/language/" | grep -q "Bosque "; then
echo -e "${GREEN}PASSED Bosque Language Check"
echo -e "${GREEN}PASSED bosque Language Check"
else
echo -e "${RED}======================================================="
echo -e "FAILED Should be able to find Bosque"
echo -e "FAILED Should be able to find bosque"
echo -e "======================================================="
exit
fi
Expand Down

0 comments on commit d796740

Please sign in to comment.