Skip to content

Commit

Permalink
Upgrade go version
Browse files Browse the repository at this point in the history
Added linter to github actions
  • Loading branch information
zvdm committed Aug 27, 2022
1 parent 002072d commit bf23800
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
build:
strategy:
matrix:
go-versio: [1.18.x, 1.19.x]
go-version: [1.18.x, 1.19.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand All @@ -21,7 +21,12 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-versio }}
go-version: ${{ matrix.go-version }}

- name: Linter
uses: golangci/golangci-lint-action@v3
with:
version: latest

- name: Vet
run: go vet ./...
Expand Down
56 changes: 27 additions & 29 deletions envs_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package envs

import (
"fmt"
"io/ioutil"
"os"
"testing"
)
Expand All @@ -12,10 +10,10 @@ func Test_Get(t *testing.T) {
_ = os.Setenv("TEST_GET_ENVS", "get_envs")
envs.ReadEnvs()
if res := envs.Get("TEST_GET_ENVS"); res != "get_envs" {
t.Error(fmt.Sprintf("expected 'get_envs', got '%s'", res))
t.Errorf("expected 'get_envs', got '%s'", res)
}
if res := envs.Get("TEST_GET_ENVS_DEFAULT", "default_string"); res != "default_string" {
t.Error(fmt.Sprintf("expected 'default_string', got '%s'", res))
t.Errorf("expected 'default_string', got '%s'", res)
}
}

Expand All @@ -24,10 +22,10 @@ func Test_GetBool(t *testing.T) {
_ = os.Setenv("TEST_GET_ENVS_BOOL", "true")
envs.ReadEnvs()
if res := envs.GetBool("TEST_GET_ENVS_BOOL"); !res {
t.Error(fmt.Sprintf("expected true, got %t", res))
t.Errorf("expected true, got %t", res)
}
if res := envs.GetBool("TEST_GET_ENVS_BOOL_DEFAULT", true); !res {
t.Error(fmt.Sprintf("expected true, got %t", res))
t.Errorf("expected true, got %t", res)
}
}

Expand All @@ -36,10 +34,10 @@ func Test_GetFloat(t *testing.T) {
_ = os.Setenv("TEST_GET_ENVS_FLOAT", "123.7645")
envs.ReadEnvs()
if res := envs.GetFloat("TEST_GET_ENVS_FLOAT"); res != float32(123.7645) {
t.Error(fmt.Sprintf("expected 123.7645, got %f", res))
t.Errorf("expected 123.7645, got %f", res)
}
if res := envs.GetFloat("TEST_GET_ENVS_FLOAT_DEFAULT", 456.123); res != float32(456.123) {
t.Error(fmt.Sprintf("expected 456.123, got %f", res))
t.Errorf("expected 456.123, got %f", res)
}
}

Expand All @@ -48,10 +46,10 @@ func Test_GetInt(t *testing.T) {
_ = os.Setenv("TEST_GET_ENVS_INT", "123")
envs.ReadEnvs()
if res := envs.GetInt("TEST_GET_ENVS_INT"); res != 123 {
t.Error(fmt.Sprintf("expected 123, got %d", res))
t.Errorf("expected 123, got %d", res)
}
if res := envs.GetInt("TEST_GET_ENVS_INT_DEFAULT", 876); res != 876 {
t.Error(fmt.Sprintf("expected 876, got %d", res))
t.Errorf("expected 876, got %d", res)
}
}

Expand All @@ -61,14 +59,14 @@ func Test_GetMap(t *testing.T) {
envs.ReadEnvs()
res := envs.GetMap("TEST_GET_ENVS_MAP")
if value := res["key1"]; value != "value1" {
t.Error(fmt.Sprintf("expected 'value1', got '%s'", value))
t.Errorf("expected 'value1', got '%s'", value)
}
if value := res["key2"]; value != "value2" {
t.Error(fmt.Sprintf("expected 'value2', got '%s'", value))
t.Errorf("expected 'value2', got '%s'", value)
}
res = envs.GetMap("TEST_GET_ENVS_MAP_DEFAULT", map[string]string{"defaultKey": "defaultValue"})
if value := res["defaultKey"]; value != "defaultValue" {
t.Error(fmt.Sprintf("expected 'defaultValue', got '%s'", value))
t.Errorf("expected 'defaultValue', got '%s'", value)
}
}

Expand All @@ -79,11 +77,11 @@ func Test_GetSlice(t *testing.T) {
expected := []string{"first", "second", "third"}
for i, value := range envs.GetSlice("TEST_GET_ENVS_SLICE") {
if value != expected[i] {
t.Error(fmt.Sprintf("expected '%s', got '%s'", expected[i], value))
t.Errorf("expected '%s', got '%s'", expected[i], value)
}
}
if res := envs.GetSlice("TEST_GET_ENVS_SLICE_DEFAULT", []string{"defaultValue"}); res[0] != "defaultValue" {
t.Error(fmt.Sprintf("expected 'defaultValue', got '%s'", res[0]))
t.Errorf("expected 'defaultValue', got '%s'", res[0])
}
}

Expand All @@ -94,12 +92,12 @@ func Test_GetSliceFloat(t *testing.T) {
expected := []float32{123.554, 64.23, 78.09876}
for i, value := range envs.GetSliceFloat("TEST_GET_ENVS_SLICE_FLOAT") {
if value != expected[i] {
t.Error(fmt.Sprintf("expected %f, got %f", expected[i], value))
t.Errorf("expected %f, got %f", expected[i], value)
}
}
res := envs.GetSliceFloat("TEST_GET_ENVS_SLICE_FLOAT_DEFAULT", []float32{456.235})
if res[0] != float32(456.235) {
t.Error(fmt.Sprintf("expected 456.235, got %f", res[0]))
t.Errorf("expected 456.235, got %f", res[0])
}
}

Expand All @@ -110,68 +108,68 @@ func Test_GetSliceInt(t *testing.T) {
expected := []int{453, 8, 124567}
for i, value := range envs.GetSliceInt("TEST_GET_ENVS_SLICE_INT") {
if value != expected[i] {
t.Error(fmt.Sprintf("expected %d, got %d", expected[i], value))
t.Errorf("expected %d, got %d", expected[i], value)
}
}
if res := envs.GetSliceInt("TEST_GET_ENVS_SLICE_INT_DEFAULT", []int{3576}); res[0] != 3576 {
t.Error(fmt.Sprintf("expected 3576, got %d", res[0]))
t.Errorf("expected 3576, got %d", res[0])
}
}

func Test_readEnvs(t *testing.T) {
envs := EnvConfig{}

{ // the .env file exists - envs are empty
_ = ioutil.WriteFile(".env", []byte{}, 0644)
_ = os.WriteFile(".env", []byte{}, 0644)
envs.ReadEnvs()
if res := envs.Get("TEST_READ_ENVS_1"); res != "" {
t.Error(fmt.Sprintf("expected empty value, got %s", res))
t.Errorf("expected empty value, got %s", res)
}
_ = os.Remove(".env")
}

{ // the .env file exists - envs are read
_ = ioutil.WriteFile(".env", []byte("# Comment\nTEST_READ_ENVS_2=read_envs_2"), 0644)
_ = os.WriteFile(".env", []byte("# Comment\nTEST_READ_ENVS_2=read_envs_2"), 0644)
envs.ReadEnvs()
if res := envs.Get("TEST_READ_ENVS_2"); res != "read_envs_2" {
t.Error(fmt.Sprintf("expected 'read_envs_2', got %s", res))
t.Errorf("expected 'read_envs_2', got %s", res)
}
_ = os.Remove(".env")
}

{ // a file with custom name exists - envs are read
_ = ioutil.WriteFile(".custom_name", []byte("TEST_READ_ENVS_3=read_envs_3"), 0644)
_ = os.WriteFile(".custom_name", []byte("TEST_READ_ENVS_3=read_envs_3"), 0644)
envs.Filepath = ".custom_name"
envs.ReadEnvs()
if res := envs.Get("TEST_READ_ENVS_3"); res != "read_envs_3" {
t.Error(fmt.Sprintf("expected 'read_envs_3', got %s", res))
t.Errorf("expected 'read_envs_3', got %s", res)
}
_ = os.Remove(".custom_name")
envs.Filepath = ""
}

{ // the .env file exists and the same env var was set - envs are read
_ = os.Setenv("TEST_READ_ENVS_4", "read_envs_4_from_env?somePara=value")
_ = ioutil.WriteFile(".env", []byte("TEST_READ_ENVS_4=read_envs_4"), 0644)
_ = os.WriteFile(".env", []byte("TEST_READ_ENVS_4=read_envs_4"), 0644)
envs.ReadEnvs()
if res := envs.Get("TEST_READ_ENVS_4"); res != "read_envs_4_from_env?somePara=value" {
t.Error(fmt.Sprintf("expected 'read_envs_4_from_env?somePara=value', got %s", res))
t.Errorf("expected 'read_envs_4_from_env?somePara=value', got %s", res)
}
_ = os.Remove(".env")
}

{ // the .env file doesn't exists - envs are empty
envs.ReadEnvs()
if res := envs.Get("TEST_READ_ENVS_5"); res != "" {
t.Error(fmt.Sprintf("expected empty value, got %s", res))
t.Errorf("expected empty value, got %s", res)
}
}

{ // the .env file doesn't exists - envs are read
_ = os.Setenv("TEST_READ_ENVS_6", "read_envs_6")
envs.ReadEnvs()
if res := envs.Get("TEST_READ_ENVS_6"); res != "read_envs_6" {
t.Error(fmt.Sprintf("expected 'read_envs_6', got %s", res))
t.Errorf("expected 'read_envs_6', got %s", res)
}
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/elsevierlabs-os/go-envs

go 1.18
go 1.19

0 comments on commit bf23800

Please sign in to comment.