Skip to content
Snippets Groups Projects
Commit a22a3d9b authored by Ivan Ivanov's avatar Ivan Ivanov
Browse files

First part of the problem is done. Testing included. Not sure if testing the...

First part of the problem is done. Testing included. Not sure if testing the output is appropriate as it contains print statements.
parents
No related branches found
No related tags found
No related merge requests found
go.mod 0 → 100644
module tree-linux
go 1.16
package main
import (
"fmt"
"os"
"path"
"sort"
)
//Counter strucutre need for presenting final results
type Counter struct {
files int
directories int
}
//Function that checks if path is directory of folder
func (counter *Counter) isDir(path string) {
stat, _ := os.Stat(path)
if stat.IsDir() {
counter.directories += 1
} else {
counter.files += 1
}
}
//Function for opening a directory returns all the names of the directories as well as their indices.
func opendirectory(base string) []string {
file, err := os.Open(base)
if err != nil {
ErrorLogger.Println(err)
}
//No need to catch the error as it might be a file not a directory.
dirnames, _ := file.Readdirnames(0)
defer file.Close()
sort.Strings(dirnames)
return dirnames
}
//Recursive function called on each directory. The function appends each path found
//to a subpath called next, checks if each path is directory or file and appends to counter.
//A print statment is based on the index of the path in the current directory.
func tree(prefix string, counter *Counter, base string) {
dirnames := opendirectory(base)
for index, name := range dirnames {
//Exclude hiddenn folders
if name[0] == '.' {
continue
}
next := path.Join(base, name)
counter.isDir(next)
if index < len(dirnames)-1 {
fmt.Println(prefix+"├──", name)
tree(prefix+"│ ", counter, next)
} else {
fmt.Println(prefix+"└──", name)
tree(prefix+" ", counter, next)
}
}
}
package main
import (
"fmt"
"testing"
)
/*
Standard assert Equals function
*/
func assertEqual(t *testing.T, a interface{}, b interface{}, message string) {
if a == b {
return
}
if len(message) == 0 {
message = fmt.Sprintf("%v != %v", a, b)
}
t.Fatal(message)
}
/*
Test isDir() simple.
*/
func TestIsDir(t *testing.T) {
counter := new(Counter)
counter.isDir("./tests")
counter.isDir("./tests/testfile")
assertEqual(t, counter.directories, 1, "")
assertEqual(t, counter.files, 1, "")
}
/*
Test isDir() simple.
*/
func TestIsDir2(t *testing.T) {
counter := new(Counter)
counter.isDir("./tests")
counter.isDir("./tests/testFolder1")
counter.isDir("./tests/testFolder2")
counter.isDir("./tests/testfile")
counter.isDir("./tests/testfile1")
counter.isDir("./tests/testfile2")
assertEqual(t, counter.directories, 3, "")
assertEqual(t, counter.files, 3, "")
}
/*
Test openDirectory() for tests directory. Should return 5 paths
*/
func TestOpenDirectory(t *testing.T) {
result := opendirectory("./tests")
assertEqual(t, len(result), 5, "")
assertEqual(t, result[0], "testFolder1", "")
assertEqual(t, result[1], "testFolder2", "")
}
main.go 0 → 100644
package main
import (
"fmt"
"log"
"os"
)
//Declaring all the variables needed
var (
ErrorLogger *log.Logger
directory string
)
func main() {
ErrorLogger = log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
directory = "."
if len(os.Args) > 1 {
directory = os.Args[1]
}
counter := new(Counter)
fmt.Println(directory)
//Start recursive tree.
tree("", counter, directory)
fmt.Printf("%d directories, %d files\n", counter.directories, counter.files)
}
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment