-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* temperature unit can be configured * tests for load config * github actions
- Loading branch information
Showing
8 changed files
with
148 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: Go | ||
|
||
on: [pull_request] | ||
|
||
jobs: | ||
|
||
build: | ||
runs-on: macos-13 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '1.21' | ||
|
||
- name: Build | ||
run: go build ./... | ||
|
||
- name: Test | ||
run: go test -v -race ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func CreateTestConfig(configContent []byte) (*os.File, error) { | ||
tmpfile, err := os.CreateTemp("", "*-pass") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if _, err := tmpfile.Write(configContent); err != nil { | ||
return nil, err | ||
} | ||
if err := tmpfile.Close(); err != nil { | ||
return nil, err | ||
} | ||
return tmpfile, nil | ||
} | ||
|
||
func TestLoadConfig(t *testing.T) { | ||
var testCases = []struct { | ||
name string | ||
configContent []byte | ||
token string | ||
interval int | ||
tempUnit string | ||
err error | ||
}{ | ||
{ | ||
"full-token", | ||
[]byte(fmt.Sprintf("token: \"1234567890\"\ninterval: 60\ntempUnit: \"C\"")), | ||
"1234567890", | ||
60, | ||
"C", | ||
nil, | ||
}, | ||
{ | ||
"missing-interval", | ||
[]byte(fmt.Sprintf("token: \"1234567890\"\ntempUnit: \"F\"")), | ||
"1234567890", | ||
0, | ||
"F", | ||
nil, | ||
}, | ||
{ | ||
"invalid-config", | ||
[]byte(`foobar-invalid`), | ||
"", | ||
0, | ||
"", | ||
&yaml.TypeError{Errors: []string{"line 1: cannot unmarshal !!str `foobar-...` into main.Config"}}}, | ||
} | ||
|
||
for _, tC := range testCases { | ||
t.Run(tC.name, func(t *testing.T) { | ||
tempFile, err := CreateTestConfig(tC.configContent) | ||
require.NoError(t, err) | ||
defer os.Remove(tempFile.Name()) | ||
|
||
cfg, err := LoadConfig(tempFile.Name()) | ||
if err == nil { | ||
assert.Equal(t, tC.token, cfg.Token) | ||
assert.Equal(t, tC.interval, cfg.Interval) | ||
assert.Equal(t, tC.tempUnit, cfg.TempUnit) | ||
} | ||
assert.Equal(t, tC.err, err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters