Skip to content

Commit

Permalink
adding unit tests, pt. 2 (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
edaniszewski committed Mar 30, 2020
1 parent 83af80a commit db050f3
Show file tree
Hide file tree
Showing 30 changed files with 2,650 additions and 615 deletions.
31 changes: 16 additions & 15 deletions sdk/errors/policy_test.go → internal/test/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package errors
package test

import (
"io/ioutil"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestPolicyViolationError(t *testing.T) {
err := NewPolicyViolationError("DeviceConfigOptional", "message")

assert.IsType(t, &PolicyViolationError{}, err)
assert.Equal(t, "DeviceConfigOptional", err.policy)
assert.Equal(t, "message", err.msg)
}

func TestPolicyViolationError_Error(t *testing.T) {
err := NewPolicyViolationError("PluginConfigRequired", "message")
out := err.Error()
// TempDir is a test utility which creates a temporary directory for testing.
// It returns the directory path as well as a function to clean up the directory
// after the test.
func TempDir(t *testing.T) (string, func()) {
dir, err := ioutil.TempDir("", "synsesdktest")
if err != nil {
t.Fatal(err)
}

assert.Equal(t, "policy violation (PluginConfigRequired): message", out)
return dir, func() {
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
}
}
16 changes: 16 additions & 0 deletions internal/test/env.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
// Synse SDK
// Copyright (c) 2019 Vapor IO
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package test

import (
Expand Down
79 changes: 27 additions & 52 deletions internal/test/file.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
// Synse SDK
// Copyright (c) 2019 Vapor IO
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package test

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
)

Expand Down Expand Up @@ -36,52 +49,14 @@ func (f FileInfo) Name() string { return f.name } // nolint
func (f FileInfo) Size() int64 { return f.size } // nolint
func (f FileInfo) Sys() interface{} { return f.sys } // nolint

// TempDir holds the current directory being used as the test directory.
// This is generated via ioutil.TempDir.
var TempDir = ""

// SetupTestDir creates a test directory if one does not already exist.
func SetupTestDir(t *testing.T) {
info, err := os.Stat(TempDir)
if err != nil {
if os.IsNotExist(err) {
dir, e := ioutil.TempDir("", "test")
if e != nil {
t.Fatal(e)
}
TempDir = dir
} else {
t.Fatal(err)
}
} else {
if !info.IsDir() {
t.Error("testDir is set, but is not a directory")
}
}
}

// ClearTestDir removes the test directory, if it exists.
func ClearTestDir(t *testing.T) {
if TempDir != "" {
err := os.RemoveAll(TempDir)
if err != nil {
t.Fatal(err)
}
}
}

// WriteTempFile is a test helper that will write the specified file to
// a test directory. This is essentially a wrapper around ioutil.WriteFile
// that ensures the test directory is in place.
func WriteTempFile(t *testing.T, filename, data string, perm os.FileMode) string {
if TempDir == "" {
SetupTestDir(t)
}

fullPath := filepath.Join(TempDir, filename)
err := ioutil.WriteFile(filepath.Join(TempDir, filename), []byte(data), perm)
if err != nil {
t.Fatal(err)
}
return fullPath
}
//// WriteTempFile is a test helper that will write the specified file to
//// a test directory. This is essentially a wrapper around ioutil.WriteFile
//// that ensures the test directory is in place.
//func WriteTempFile(t *testing.T, filename, data string, perm os.FileMode) string {
// fullPath := filepath.Join(TempDir, filename)
// err := ioutil.WriteFile(filepath.Join(TempDir, filename), []byte(data), perm)
// if err != nil {
// t.Fatal(err)
// }
// return fullPath
//}
16 changes: 16 additions & 0 deletions internal/test/utils.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
// Synse SDK
// Copyright (c) 2019 Vapor IO
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package test

import (
Expand Down
16 changes: 16 additions & 0 deletions sdk/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@ func TestLoader_Scan_NoMergedData(t *testing.T) {
assert.Equal(t, &TestData{}, td)
}

func TestLoader_Scan_NoMergedOptional(t *testing.T) {
type TestData struct {
Value string
}
td := &TestData{}

// Create a loader, do not specify any merged data.
loader := Loader{}
loader.policy = policy.Optional

// Scan the config into the TestData struct.
err := loader.Scan(td)
assert.NoError(t, err)
assert.Equal(t, &TestData{}, td)
}

func TestLoader_checkOverrides_noOverride(t *testing.T) {
loader := Loader{
FileName: "placeholder",
Expand Down
Loading

0 comments on commit db050f3

Please sign in to comment.