-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsystem_test.go
More file actions
126 lines (115 loc) · 3.34 KB
/
system_test.go
File metadata and controls
126 lines (115 loc) · 3.34 KB
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
122
123
124
125
126
// Copyright (c) 2023 Benjamin Borbe All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main_test
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
"github.com/onsi/gomega/ghttp"
)
var pathToServerBinary string
var serverSession *gexec.Session
var _ = BeforeSuite(func() {
var err error
pathToServerBinary, err = gexec.Build("github.com/bborbe/git-sync")
Expect(err).NotTo(HaveOccurred())
})
var _ = AfterSuite(func() {
gexec.CleanupBuildArtifacts()
})
type args map[string]string
func (a args) list() []string {
var result []string
for k, v := range a {
if len(v) == 0 {
result = append(result, fmt.Sprintf("-%s", k))
} else {
result = append(result, fmt.Sprintf("-%s=%s", k, v))
}
}
return result
}
var _ = Describe("git-sync", func() {
var err error
It("returns with exitcode != 0 if no parameters have been given", func() {
serverSession, err = gexec.Start(
exec.Command(pathToServerBinary),
GinkgoWriter,
GinkgoWriter,
)
Expect(err).To(BeNil())
serverSession.Wait(time.Second)
Expect(serverSession.ExitCode()).NotTo(Equal(0))
})
Context("when validating parameters", func() {
var validargs args
var targetDirectory string
BeforeEach(func() {
targetDirectory, err = ioutil.TempDir("", "git-sync")
Expect(err).To(BeNil())
validargs = map[string]string{
"logtostderr": "",
"v": "0",
"repo": "http://github.com/bborbe/dotfiles.git",
"one-time": "",
"dest": targetDirectory,
}
})
AfterEach(func() {
_ = os.RemoveAll(targetDirectory)
})
It("returns with exitcode == 0", func() {
serverSession, err = gexec.Start(
exec.Command(pathToServerBinary, validargs.list()...), // #nosec G204
GinkgoWriter,
GinkgoWriter,
)
Expect(err).To(BeNil())
serverSession.Wait(5 * time.Second)
Expect(serverSession.ExitCode()).To(Equal(0))
_, err = os.Stat(targetDirectory)
Expect(os.IsNotExist(err)).To(BeFalse())
})
Context("and url parameter", func() {
var server *ghttp.Server
BeforeEach(func() {
server = ghttp.NewServer()
server.RouteToHandler(http.MethodGet, "/", ghttp.RespondWith(http.StatusOK, "OK"))
})
AfterEach(func() {
serverSession.Interrupt()
Eventually(serverSession).Should(gexec.Exit())
server.Close()
})
It("calls the url", func() {
validargs["callback-url"] = server.URL()
serverSession, err = gexec.Start(
exec.Command(pathToServerBinary, validargs.list()...), // #nosec G204
GinkgoWriter,
GinkgoWriter,
)
Expect(err).To(BeNil())
serverSession.Wait(5 * time.Second)
Expect(serverSession.ExitCode()).To(Equal(0))
Expect(len(server.ReceivedRequests())).To(Equal(1))
})
It("calls the url with env", func() {
delete(validargs, "callback-url")
command := exec.Command(pathToServerBinary, validargs.list()...) // #nosec G204
command.Env = append(os.Environ(), fmt.Sprintf("CALLBACK_URL=%s", server.URL()))
serverSession, err = gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).To(BeNil())
serverSession.Wait(5 * time.Second)
Expect(serverSession.ExitCode()).To(Equal(0))
Expect(len(server.ReceivedRequests())).To(Equal(1))
})
})
})
})