Skip to content

Commit 444bbd0

Browse files
authored
Merge pull request #5 from Attumm/test-coverage
Added codecov and some additional tests
2 parents 74f3d7d + a52317b commit 444bbd0

File tree

7 files changed

+295
-59
lines changed

7 files changed

+295
-59
lines changed

.github/workflows/race-test.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Race Detection Tests
2+
on:
3+
push:
4+
branches: [ main ]
5+
pull_request:
6+
branches: [ main ]
7+
jobs:
8+
test:
9+
name: Race Detector
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v3
14+
- name: Set up Go
15+
uses: actions/setup-go@v4
16+
with:
17+
go-version: '1.21'
18+
- name: Run race detector
19+
run: |
20+
go test -race -covermode=atomic ./...

.github/workflows/test-coverage.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Test and Coverage
2+
on:
3+
push:
4+
branches: [ main ]
5+
pull_request:
6+
branches: [ main ]
7+
jobs:
8+
test:
9+
name: Test and Coverage
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v3
14+
- name: Set up Go
15+
uses: actions/setup-go@v4
16+
with:
17+
go-version: '1.21'
18+
- name: Run tests with coverage
19+
run: |
20+
go test -coverprofile=coverage.txt ./...
21+
- name: Upload coverage reports to Codecov
22+
uses: codecov/codecov-action@v5
23+
with:
24+
token: ${{ secrets.CODECOV_TOKEN }}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Settingo
22
[![Go Reference](https://pkg.go.dev/badge/github.com/Attumm/settingo/settingo.svg)](https://pkg.go.dev/github.com/Attumm/settingo/settingo)
33
[![Go Report Card](https://goreportcard.com/badge/github.com/Attumm/settingo)](https://goreportcard.com/report/github.com/Attumm/settingo)
4-
4+
[![codecov](https://codecov.io/gh/Attumm/settingo/branch/main/graph/badge.svg)](https://codecov.io/gh/Attumm/settingo)
55

66
## _Settings should be simple, and with settingo it is._
77

settingo/settingo_test.go

Lines changed: 72 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,23 @@
11
package settingo
22

33
import (
4+
"net/url"
45
"os"
56
"reflect"
67
"testing"
78
)
89

9-
type TestConfig struct {
10-
Foobar string `settingo:"help text for foobar"`
11-
FoobarInt int `settingo:"help text for FoobarInt"`
12-
FoobarBool bool `settingo:"help text for FoobarBool"`
13-
FoobarMap map[string][]string `settingo:"help text FoobarMap"`
14-
}
15-
16-
func Test_struct_types_default(t *testing.T) {
17-
18-
expected := "default_value_for_foobar"
19-
expectedInt := 42
20-
expectedBool := true
21-
expectedMap := make(map[string][]string)
22-
23-
expectedMap["foo"] = []string{"bar"}
24-
expectedMap["foo1"] = []string{"bar1", "bar2"}
25-
26-
config := &TestConfig{
27-
Foobar: expected,
28-
FoobarInt: expectedInt,
29-
FoobarBool: expectedBool,
30-
FoobarMap: expectedMap,
31-
}
32-
33-
SETTINGS.LoadStruct(config)
34-
35-
if config.Foobar != expected {
36-
t.Error(config.Foobar, " != ", expected)
37-
}
38-
39-
if config.FoobarInt != expectedInt {
40-
t.Error(config.FoobarInt, " != ", expectedInt)
41-
}
42-
43-
if config.FoobarBool != expectedBool {
44-
t.Error(config.FoobarBool, " != ", expectedBool)
45-
}
46-
47-
if !reflect.DeepEqual(config.FoobarMap, expectedMap) {
48-
t.Error(config.FoobarMap, " != ", expectedMap)
49-
}
50-
51-
}
52-
5310
type ExampleConfig struct {
54-
Foobar string `settingo:"help text for foobar"`
55-
FoobarInt int `settingo:"help text for FoobarInt"`
56-
FoobarBool bool `settingo:"help text for FoobarBool"`
57-
FoobarMap map[string][]string `settingo:"help text FoobarMap"`
11+
Foobar string `settingo:"help text for foobar"`
12+
FoobarInt int `settingo:"help text for FoobarInt"`
13+
FoobarBool bool `settingo:"help text for FoobarBool"`
14+
FoobarMap map[string][]string `settingo:"help text FoobarMap"`
15+
FoobarSlice []string `settingo:"help text for FoobarSlice"`
16+
FooParse string `settingo:"help text for FooParse"`
17+
FooParseInt int `settingo:"help text for FooParseInt"`
5818
}
5919

6020
func Test_struct_types_os_env(t *testing.T) {
61-
6221
expected := "other value"
6322
os.Setenv("FOOBAR", expected)
6423
defaultStr := "default value"
@@ -78,14 +37,59 @@ func Test_struct_types_os_env(t *testing.T) {
7837
expectedMap["foo"] = []string{"bar"}
7938
expectedMap["foo1"] = []string{"bar1", "bar2"}
8039

40+
os.Setenv("FOOBARSLICE", "item1,item2,item3")
41+
expectedSlice := []string{"item1", "item2", "item3"}
42+
defaultSlice := []string{}
43+
44+
os.Setenv("FOOPARSE", "postgres://user:[email protected]:5432/mydb")
45+
expectedFooParse := "database.example.com"
46+
defaultFooParse := "foobar"
47+
48+
SetParsed("FOOPARSE", defaultFooParse, "database hostname", func(input string) string {
49+
u, err := url.Parse(input)
50+
if err != nil {
51+
return input
52+
}
53+
return u.Hostname()
54+
})
55+
56+
Set("FOOBAR", defaultStr, "help text for foobar")
57+
SetString("FOOBAR", defaultStr, "help text for foobar")
58+
SetInt("FOOBARINT", defaultInt, "help text for FoobarInt")
59+
SetBool("FOOBARBOOL", defaultBool, "help text for FoobarBool")
60+
SetMap("FOOBARMAP", defaultMap, "help text FoobarMap")
61+
SetSlice("FOOBARSLICE", defaultSlice, "help text for FoobarSlice", ",")
62+
8163
config := &ExampleConfig{
82-
Foobar: defaultStr,
83-
FoobarInt: defaultInt,
84-
FoobarBool: defaultBool,
85-
FoobarMap: defaultMap,
64+
Foobar: defaultStr,
65+
FoobarInt: defaultInt,
66+
FoobarBool: defaultBool,
67+
FoobarMap: defaultMap,
68+
FoobarSlice: defaultSlice,
69+
FooParse: defaultFooParse,
8670
}
8771

88-
SETTINGS.ParseTo(config)
72+
ParseTo(config)
73+
74+
if Get("FOOBAR") != expected {
75+
t.Error(Get("FOOBAR"), " != ", expected)
76+
}
77+
78+
if GetInt("FOOBARINT") != expectedInt {
79+
t.Error(GetInt("FOOBARINT"), " != ", expectedInt)
80+
}
81+
82+
if GetBool("FOOBARBOOL") != expectedBool {
83+
t.Error(GetBool("FOOBARBOOL"), " != ", expectedBool)
84+
}
85+
86+
if !reflect.DeepEqual(GetMap("FOOBARMAP"), expectedMap) {
87+
t.Error(GetMap("FOOBARMAP"), " != ", expectedMap)
88+
}
89+
90+
if !reflect.DeepEqual(GetSlice("FOOBARSLICE"), expectedSlice) {
91+
t.Error(GetSlice("FOOBARSLICE"), " != ", expectedSlice)
92+
}
8993

9094
if config.Foobar != expected {
9195
t.Error(config.Foobar, " != ", expected)
@@ -103,9 +107,20 @@ func Test_struct_types_os_env(t *testing.T) {
103107
t.Error(config.FoobarMap, " != ", expectedMap)
104108
}
105109

110+
if !reflect.DeepEqual(config.FoobarSlice, expectedSlice) {
111+
t.Error(config.FoobarSlice, " != ", expectedSlice)
112+
}
113+
114+
if config.FooParse != expectedFooParse {
115+
t.Error(config.FooParse, " != ", expectedFooParse)
116+
}
117+
106118
// Cleanup
107119
os.Unsetenv("FOOBAR")
108-
os.Unsetenv("FOOBAR_INT")
109-
os.Unsetenv("FOOBAR_BOOL")
110-
os.Unsetenv("FOOBAR_MAP")
120+
os.Unsetenv("FOOBARINT")
121+
os.Unsetenv("FOOBARBOOL")
122+
os.Unsetenv("FOOBARMAP")
123+
os.Unsetenv("FOOBARSLICE")
124+
os.Unsetenv("FOOPARSE")
125+
os.Unsetenv("FOOPARSEINT")
111126
}

settingo/settings_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,78 @@
11
package settingo
2+
3+
import (
4+
"net/url"
5+
"os"
6+
"reflect"
7+
"testing"
8+
)
9+
10+
type TestConfig struct {
11+
Foobar string `settingo:"help text for foobar"`
12+
FoobarInt int `settingo:"help text for FoobarInt"`
13+
FoobarBool bool `settingo:"help text for FoobarBool"`
14+
FoobarMap map[string][]string `settingo:"help text FoobarMap"`
15+
FoobarSlice []string `settingo:"help text for FoobarSlice"`
16+
FooParse string `settingo:"help text for FooParse"`
17+
}
18+
19+
func Test_struct_types_default(t *testing.T) {
20+
21+
expected := "default_value_for_foobar"
22+
expectedInt := 42
23+
expectedBool := true
24+
expectedMap := make(map[string][]string)
25+
26+
expectedMap["foo"] = []string{"bar"}
27+
expectedMap["foo1"] = []string{"bar1", "bar2"}
28+
29+
os.Setenv("FOOBARSLICE", "item1,item2,item3")
30+
expectedSlice := []string{"item1", "item2", "item3"}
31+
32+
os.Setenv("FOOPARSE", "postgres://user:[email protected]:5432/mydb")
33+
expectedFooParse := "database.example.com"
34+
35+
// Parser that extracts hostname from database URL
36+
SETTINGS.SetParsed("FOOPARSE", "", "database hostname", func(input string) string {
37+
u, err := url.Parse(input)
38+
if err != nil {
39+
return input
40+
}
41+
return u.Hostname()
42+
})
43+
44+
config := &TestConfig{
45+
Foobar: expected,
46+
FoobarInt: expectedInt,
47+
FoobarBool: expectedBool,
48+
FoobarMap: expectedMap,
49+
FoobarSlice: expectedSlice,
50+
FooParse: expectedFooParse,
51+
}
52+
53+
SETTINGS.LoadStruct(config)
54+
55+
if config.Foobar != expected {
56+
t.Error(config.Foobar, " != ", expected)
57+
}
58+
59+
if config.FoobarInt != expectedInt {
60+
t.Error(config.FoobarInt, " != ", expectedInt)
61+
}
62+
63+
if config.FoobarBool != expectedBool {
64+
t.Error(config.FoobarBool, " != ", expectedBool)
65+
}
66+
67+
if !reflect.DeepEqual(config.FoobarMap, expectedMap) {
68+
t.Error(config.FoobarMap, " != ", expectedMap)
69+
}
70+
71+
if !reflect.DeepEqual(config.FoobarSlice, expectedSlice) {
72+
t.Error(config.FoobarSlice, " != ", expectedSlice)
73+
}
74+
75+
if config.FooParse != expectedFooParse {
76+
t.Error(config.FooParse, " != ", expectedFooParse)
77+
}
78+
}

settingo/strings_operations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func ParseLineToMap(s string) map[string][]string {
2828
for _, item := range items {
2929
key, values, err := parseKeyValue(item)
3030
if err {
31-
fmt.Println("Unable to parse line, discarded:", item)
31+
fmt.Println("Settingo: Unable to parse line, discarded:", item)
3232
continue
3333
}
3434
parsed[key] = values

0 commit comments

Comments
 (0)