forked from getgauge/html-report
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlReport_test.go
93 lines (74 loc) · 2.83 KB
/
htmlReport_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2015 ThoughtWorks, Inc.
// This file is part of getgauge/html-report.
// getgauge/html-report 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.
// getgauge/html-report 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 getgauge/html-report. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"fmt"
"os"
"path/filepath"
"reflect"
"testing"
"time"
"github.com/getgauge/html-report/env"
helper "github.com/getgauge/html-report/test_helper"
)
var now = time.Now()
type testNameGenerator struct {
}
func (T testNameGenerator) randomName() string {
return now.Format(timeFormat)
}
func TestGetReportsDirectory(t *testing.T) {
userSetReportsDir := filepath.Join(os.TempDir(), randomName())
os.Setenv(env.GaugeReportsDirEnvName, userSetReportsDir)
expectedReportsDir := filepath.Join(userSetReportsDir, htmlReport)
defer os.RemoveAll(userSetReportsDir)
reportsDir := getReportsDirectory(nil)
if reportsDir != expectedReportsDir {
t.Errorf("Expected reportsDir == %s, got: %s\n", expectedReportsDir, reportsDir)
}
if !helper.FileExists(expectedReportsDir) {
t.Errorf("Expected %s report directory doesn't exist", expectedReportsDir)
}
}
func TestGetReportsDirectoryWithOverrideFlag(t *testing.T) {
userSetReportsDir := filepath.Join(os.TempDir(), randomName())
os.Setenv(env.GaugeReportsDirEnvName, userSetReportsDir)
os.Setenv(env.OverwriteReportsEnvProperty, "true")
nameGen := &testNameGenerator{}
expectedReportsDir := filepath.Join(userSetReportsDir, htmlReport, nameGen.randomName())
defer os.RemoveAll(userSetReportsDir)
reportsDir := getReportsDirectory(nameGen)
if reportsDir != expectedReportsDir {
t.Errorf("Expected reportsDir == %s, got: %s\n", expectedReportsDir, reportsDir)
}
if !helper.FileExists(expectedReportsDir) {
t.Errorf("Expected %s report directory doesn't exist", expectedReportsDir)
}
}
func randomName() string {
return fmt.Sprintf("%d", time.Now().UnixNano())
}
func TestCreatingReportShouldOverwriteReportsBasedOnEnv(t *testing.T) {
os.Setenv(env.OverwriteReportsEnvProperty, "true")
nameGen := getNameGen()
if nameGen != nil {
t.Errorf("Expected nameGen == nil, got %s", nameGen)
}
os.Setenv(env.OverwriteReportsEnvProperty, "false")
nameGen = getNameGen()
switch nameGen.(type) {
case timeStampedNameGenerator:
default:
t.Errorf("Expected nameGen to be type timeStampedNameGenerator, got %s", reflect.TypeOf(nameGen))
}
}