Skip to content

Commit 4ef5bea

Browse files
i forgot to add the tests themselves
1 parent 189a865 commit 4ef5bea

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package tests
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/blackmarllboro/create-project-struct/internal/pkg/args"
8+
)
9+
10+
func TestGetProjectName(t *testing.T) {
11+
t.Log("no project name provided")
12+
{
13+
projectName, _, err := args.GetProjectName()
14+
if err != nil {
15+
t.Fatalf("Expected an error, got nil")
16+
}
17+
18+
if projectName == "" {
19+
t.Fatalf("Expected empty project name, got '%s'", projectName)
20+
}
21+
}
22+
23+
t.Log("projectName is \".\"")
24+
{
25+
os.Args = []string{"cmd", "."}
26+
projectName, isCurrentDir, err := args.GetProjectName()
27+
if err != nil {
28+
t.Fatalf("Expected an error, got nil")
29+
}
30+
31+
wd, err := os.Getwd()
32+
if err != nil {
33+
t.Fatalf("Fail to get current directory: %s", err.Error())
34+
}
35+
36+
if projectName != wd {
37+
t.Fatalf("Expected project name to be: %s, got: %s", wd, projectName)
38+
}
39+
40+
if !isCurrentDir {
41+
t.Fatalf("Expected isCurrentDir to be true, got false")
42+
}
43+
}
44+
45+
t.Log("project name is a valid directory path")
46+
{
47+
testProjectName := "test_project"
48+
os.Args = []string{"cmd", testProjectName}
49+
projectName, isCurrentDir, err := args.GetProjectName()
50+
if err != nil {
51+
t.Fatalf("Expected an error, got nil")
52+
}
53+
54+
if projectName != testProjectName {
55+
t.Fatalf("Expected project name to be '%s', got '%s'", testProjectName, projectName)
56+
}
57+
58+
if isCurrentDir {
59+
t.Fatalf("Expected isCurrentDir to be false, got true")
60+
}
61+
}
62+
63+
t.Log("SUCCESS")
64+
}

pkg/version/tests/version_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package tests
2+
3+
import (
4+
"github.com/blackmarllboro/create-project-struct/pkg/version"
5+
"regexp"
6+
"strconv"
7+
"strings"
8+
"testing"
9+
)
10+
11+
func TestGetVersion(t *testing.T) {
12+
v, err := version.GoVersion()
13+
if err != nil {
14+
t.Fatalf("GoVersion() returned an error: %v", err)
15+
}
16+
17+
var versionParts []string
18+
19+
t.Log("verify that the version string is in the correct format")
20+
{
21+
re := regexp.MustCompile(`go \d+\.\d+(\.\d+)?`)
22+
if !re.MatchString(v) {
23+
t.Fatalf("GoVersion() returned an invalid version string: %s", v)
24+
}
25+
}
26+
27+
t.Log("verify that the version string is properly formatted")
28+
{
29+
versionDigits := strings.TrimPrefix(v, "go")
30+
versionParts = strings.Split(versionDigits, ".")
31+
if len(versionParts) < 2 || len(versionParts) > 3 {
32+
t.Fatalf("GoVersion() returned an invalid version string: %s", v)
33+
}
34+
}
35+
36+
t.Log("verify that the version string starts with \"go\"")
37+
{
38+
if !strings.HasPrefix(v, "go") {
39+
t.Fatalf("GoVersion() returned an invalid version string: %s", v)
40+
}
41+
}
42+
43+
t.Log("verify that the patch version number, if present, is an integer")
44+
{
45+
if len(versionParts) == 3 {
46+
patch := versionParts[2]
47+
if _, err := strconv.Atoi(patch); err != nil {
48+
t.Fatalf("GoVersion() returned an invalid patch version number: %s", patch)
49+
}
50+
}
51+
}
52+
53+
t.Log("SUCCESS")
54+
}

0 commit comments

Comments
 (0)