tests: add more cli tests

This commit is contained in:
Zane Schaffer 2022-08-19 14:53:09 -07:00
parent 66ca2afc78
commit fbbe537771
8 changed files with 77 additions and 8 deletions

17
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,17 @@
{
"sqltools.connections": [
{
"mysqlOptions": {
"authProtocol": "default"
},
"previewLimit": 50,
"server": "localhost",
"port": 3306,
"askForPassword": true,
"driver": "MySQL",
"name": "snippetbox",
"database": "snippetbox",
"username": "root"
}
]
}

9
cli.go
View File

@ -45,7 +45,6 @@ func run() error {
if err != nil {
return fmt.Errorf("failed to get config (%q) %w", *configPath, err)
}
inputFilePaths, err := getInputFilePaths(cfg.InputDirPath)
if err != nil {
return fmt.Errorf("failed to get input file paths (%q) %w", cfg.InputDirPath, err)
@ -118,7 +117,7 @@ func getTemplate(templatePath string) (*template.Template, error) {
return t, nil
}
// getInputFilePaths returns file paths to every .md in input directory
// getInputFilePaths returns file paths to every .md in input directory in reverse alphabetical order
func getInputFilePaths(inputDirPath string) ([]string, error) {
var inputFilePaths []string
@ -129,11 +128,15 @@ func getInputFilePaths(inputDirPath string) ([]string, error) {
defer inputDir.Close()
inputFiles, err := inputDir.Readdir(-1)
inputFiles, err := inputDir.ReadDir(-1)
if err != nil {
return nil, fmt.Errorf("failed to read inputDir %w", err)
}
if len(inputFiles) == 0 {
return nil, errors.New("inputDirPath is empty")
}
for _, file := range inputFiles {
if !file.IsDir() && file.Name()[0] != '.' {
inputFilePaths = append(inputFilePaths, filepath.Join(inputDirPath, file.Name()))

View File

@ -1,22 +1,67 @@
package main
import (
"html/template"
"reflect"
"testing"
)
func TestRun(t *testing.T) {
}
func TestParseInput(t *testing.T) {
}
func TestGetTemplate(t *testing.T) {
got, err := getTemplate("./testdata/template.html")
if err != nil {
t.Errorf("failed to get template: %v", err)
}
want, _ := template.ParseFiles("./testdata/template.html")
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
}
func TestGetInputFilePaths(t *testing.T) {
t.Run("correctly return file paths to markdown files in input directory in reverse order", func(t *testing.T) {
got, err := getInputFilePaths("./testdata/src")
if err != nil {
t.Errorf("failed to return file paths: %v", err)
}
want := []string{
"testdata/src/test3.md",
"testdata/src/test2.md",
"testdata/src/test1.md",
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
})
t.Run("return empty string array and err when passed path with no markdown files", func(t *testing.T) {
_, err := getInputFilePaths("./testdata/empty")
if err == nil {
t.Errorf("error is empty, want: inputDirPath is empty")
}
})
t.Run("return error when passed a broken path", func(t *testing.T) {
_, err := getInputFilePaths("./testdata/path/does/not/exist")
if err == nil {
t.Errorf("error is empty, want: failed to open")
}
})
}
func TestGetConfig(t *testing.T) {
t.Run("correctly parse passed config", func(t *testing.T) {
got, err := getConfig("./testdata/jenga.toml")
if err != nil {
t.Errorf("failed to parse config: %v", err)
@ -39,6 +84,5 @@ func TestGetConfig(t *testing.T) {
if err == nil {
t.Errorf("should return error when passed a bad path")
}
})
}

5
go.mod
View File

@ -9,4 +9,7 @@ require (
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
)
require golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664 // indirect
require (
github.com/bluele/adblock v0.0.0-20150928111208-97547036a6ec // indirect
golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664 // indirect
)

2
go.sum
View File

@ -1,5 +1,7 @@
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/bluele/adblock v0.0.0-20150928111208-97547036a6ec h1:bDC3KtBzO9PhiPl+SYkMnwgzMc4YjfQDnz1nmMX4Tlo=
github.com/bluele/adblock v0.0.0-20150928111208-97547036a6ec/go.mod h1:KpJbk0iUmfxhUj4NXBLYjg6E5RCApkovVJ+SPY/hcaA=
github.com/gomarkdown/markdown v0.0.0-20220731190611-dcdaee8e7a53 h1:JguE3sS3yLzLiCTCnsmzVFuTvTMDJALbzCgurwY5G/0=
github.com/gomarkdown/markdown v0.0.0-20220731190611-dcdaee8e7a53/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
github.com/rjeczalik/notify v0.9.2 h1:MiTWrPj55mNDHEiIX5YUSKefw/+lCQVoAFmD6oQm5w8=

0
testdata/src/test2.md vendored Normal file
View File

0
testdata/src/test3.md vendored Normal file
View File