1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
// Tideland Go Library - Audit - Unit Tests
//
// Copyright (C) 2013-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package audit_test
//--------------------
// IMPORTS
//--------------------
import (
"os"
"testing"
"github.com/tideland/golib/audit"
)
//--------------------
// TESTS
//--------------------
// TestTempDirCreate tests the creation of temporary directories.
func TestTempDirCreate(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
testDir := func(dir string) {
fi, err := os.Stat(dir)
assert.Nil(err)
assert.True(fi.IsDir())
assert.Equal(fi.Mode().Perm(), os.FileMode(0700))
}
td := audit.NewTempDir(assert)
assert.NotNil(td)
defer td.Restore()
tds := td.String()
assert.NotEmpty(tds)
testDir(tds)
sda := td.Mkdir("subdir", "foo")
assert.NotEmpty(sda)
testDir(sda)
sdb := td.Mkdir("subdir", "bar")
assert.NotEmpty(sdb)
testDir(sdb)
}
// TestTempDirRestore tests the restoring of temporary created
// directories.
func TestTempDirRestore(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
td := audit.NewTempDir(assert)
assert.NotNil(td)
tds := td.String()
fi, err := os.Stat(tds)
assert.Nil(err)
assert.True(fi.IsDir())
td.Restore()
fi, err = os.Stat(tds)
assert.ErrorMatch(err, "stat .* no such file or directory")
}
// TestEnvVarsSet tests the setting of temporary environment variables.
func TestEnvVarsSet(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
testEnv := func(key, value string) {
v := os.Getenv(key)
assert.Equal(v, value)
}
ev := audit.NewEnvVars(assert)
assert.NotNil(ev)
defer ev.Restore()
ev.Set("TESTING_ENV_A", "FOO")
testEnv("TESTING_ENV_A", "FOO")
ev.Set("TESTING_ENV_B", "BAR")
testEnv("TESTING_ENV_B", "BAR")
ev.Unset("TESTING_ENV_A")
testEnv("TESTING_ENV_A", "")
}
// TestEnvVarsREstore tests the restoring of temporary set environment
// variables.
func TestEnvVarsRestore(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
testEnv := func(key, value string) {
v := os.Getenv(key)
assert.Equal(v, value)
}
ev := audit.NewEnvVars(assert)
assert.NotNil(ev)
path := os.Getenv("PATH")
assert.NotEmpty(path)
ev.Set("PATH", "/foo:/bar/bin")
testEnv("PATH", "/foo:/bar/bin")
ev.Set("PATH", "/bar:/foo:/yadda/bin")
testEnv("PATH", "/bar:/foo:/yadda/bin")
ev.Restore()
testEnv("PATH", path)
}
// EOF
|