-
Notifications
You must be signed in to change notification settings - Fork 41
/
htmlReport_test.go
121 lines (103 loc) · 3.67 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*----------------------------------------------------------------
* Copyright (c) ThoughtWorks, Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE in the project root for license information.
*----------------------------------------------------------------*/
package main
import (
"fmt"
"os"
"path/filepath"
"reflect"
"runtime"
"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))
}
}
func TestCreateReportExecutableFileShouldCreateExecFile(t *testing.T) {
isSaveExecutionResultDisabled = func() bool { return false }
exPath := filepath.Join(os.TempDir(), "html-report")
exTargetFileName := "html-report-target"
if runtime.GOOS == "windows" {
exTargetFileName = "html-report-target.bat"
}
exTarget := filepath.Join(os.TempDir(), exTargetFileName)
_, err := os.Create(exPath)
if err != nil {
t.Errorf("could not create %s. %s", exPath, err.Error())
}
defer os.Remove(exPath)
defer os.Remove(exTarget)
createReportExecutableFile(exPath, exTarget)
if !fileExists(exTarget) {
t.Errorf("Could not create a symlink of src: %s to dst: %s", exPath, exTarget)
}
}
func TestCreateReportExecutableFileShouldNotCreateExecFile(t *testing.T) {
isSaveExecutionResultDisabled = func() bool { return true }
exPath := filepath.Join(os.TempDir(), "html-report")
exTarget := filepath.Join(os.TempDir(), "html-report-target")
_, err := os.Create(exPath)
if err != nil {
t.Errorf("could not create %s. %s", exPath, err.Error())
}
defer os.Remove(exPath)
defer os.Remove(exTarget)
defer os.Unsetenv(env.SaveExecutionResult)
createReportExecutableFile(exPath, exTarget)
if fileExists(exTarget) {
t.Errorf("Expected not to create a symlink of src: %s to dst: %s", exPath, exTarget)
}
}