-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjsonReport_test.go
77 lines (58 loc) · 1.83 KB
/
jsonReport_test.go
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
package main
import (
"fmt"
"os"
"path/filepath"
"testing"
"time"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
type MySuite struct{}
var _ = Suite(&MySuite{})
var now = time.Now()
type testNameGenerator struct {
}
func (T testNameGenerator) randomName() string {
return now.Format(timeFormat)
}
func (s *MySuite) TestCreatingReport(c *C) {
reportDir := filepath.Join(os.TempDir(), randomName())
defer os.RemoveAll(reportDir)
finalReportDir, err := createJSONReport(reportDir, make([]byte, 0), nil)
c.Assert(err, IsNil)
expectedFinalReportDir := filepath.Join(reportDir, jsonReport)
c.Assert(finalReportDir, Equals, expectedFinalReportDir)
verifyJSONReportFileIsCopied(expectedFinalReportDir, c)
}
func (s *MySuite) TestCreatingReportWithNoOverWrite(c *C) {
reportDir := filepath.Join(os.TempDir(), randomName())
defer os.RemoveAll(reportDir)
nameGen := testNameGenerator{}
finalReportDir, err := createJSONReport(reportDir, make([]byte, 0), nameGen)
c.Assert(err, IsNil)
expectedFinalReportDir := filepath.Join(reportDir, jsonReport, nameGen.randomName())
c.Assert(finalReportDir, Equals, expectedFinalReportDir)
verifyJSONReportFileIsCopied(expectedFinalReportDir, c)
}
func randomName() string {
return fmt.Sprintf("%d", time.Now().UnixNano())
}
func verifyJSONReportFileIsCopied(dest string, c *C) {
c.Assert(fileExists(filepath.Join(dest, jsonReportFile)), Equals, true)
}
func fileExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
return !os.IsNotExist(err)
}
func (s *MySuite) TestCreatingReportShouldOverwriteReportsBasedOnEnv(c *C) {
os.Setenv(overwriteReportsEnvProperty, "true")
nameGen := getNameGen()
c.Assert(nameGen, Equals, nil)
os.Setenv(overwriteReportsEnvProperty, "false")
nameGen = getNameGen()
c.Assert(nameGen, Equals, timeStampedNameGenerator{})
}