-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest.go
227 lines (189 loc) · 5.88 KB
/
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package commands
import (
"fmt"
"github.com/codecrafters-io/cli/internal/utils"
logstream_consumer "github.com/codecrafters-io/logstream/consumer"
"github.com/fatih/color"
wordwrap "github.com/mitchellh/go-wordwrap"
cp "github.com/otiai10/copy"
"io"
"io/ioutil"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
)
func TestCommand() int {
repoDir, err := getRepositoryDir()
if err != nil {
return 1
}
codecraftersRemote, err := utils.IdentifyGitRemote(repoDir)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
return 1
}
tmpDir, err := copyRepositoryDirToTempDir(repoDir)
if err != nil {
return 1
}
tempBranchName := "cli-test-" + strconv.FormatInt(time.Now().UnixMilli(), 10)
err = checkoutNewBranch(tempBranchName, tmpDir)
if err != nil {
return 1
}
tempCommitSha, err := commitChanges(tmpDir, fmt.Sprintf("CLI tests (%s)", tempBranchName))
if err != nil {
return 1
}
// Place this before the push so that it "feels" fast
fmt.Println("Running tests on your codebase. Streaming logs...")
err = pushBranchToRemote(tmpDir)
if err != nil {
return 1
}
codecraftersClient := utils.NewCodecraftersClient(codecraftersRemote.CodecraftersServerURL())
createSubmissionResponse, err := codecraftersClient.CreateSubmission(codecraftersRemote.CodecraftersRepositoryId(), tempCommitSha)
if err != nil {
return 1
}
if createSubmissionResponse.IsError {
fmt.Fprintf(os.Stderr, "failed to create submission: %s", createSubmissionResponse.ErrorMessage)
return 1
}
if createSubmissionResponse.OnInitSuccessMessage != "" {
fmt.Println("")
wrapped := wordwrap.WrapString(createSubmissionResponse.OnInitSuccessMessage, 79)
for _, line := range strings.Split(wrapped, "\n") {
fmt.Println(fmt.Sprintf("\033[1;92m%s\033[0m", line))
}
}
if createSubmissionResponse.OnInitWarningMessage != "" {
fmt.Println("")
wrapped := wordwrap.WrapString(createSubmissionResponse.OnInitWarningMessage, 79)
for _, line := range strings.Split(wrapped, "\n") {
fmt.Println(fmt.Sprintf("\033[31m%s\033[0m", line))
}
}
fmt.Println("")
err = streamLogs(createSubmissionResponse.LogstreamUrl)
if err != nil {
return 1
}
fetchSubmissionResponse, err := codecraftersClient.FetchSubmission(createSubmissionResponse.Id)
if err != nil {
// TODO: Notify sentry
red := color.New(color.FgRed).SprintFunc()
fmt.Fprintln(os.Stderr, red(err.Error()))
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, red("We couldn't fetch the results of your submission. Please try again?"))
fmt.Fprintln(os.Stderr, red("Let us know at [email protected] if this error persists."))
return 1
}
if fetchSubmissionResponse.Status == "failure" {
fmt.Println("")
fmt.Println(createSubmissionResponse.OnFailureMessage)
return 1
}
if fetchSubmissionResponse.Status == "success" {
fmt.Println("")
fmt.Println(createSubmissionResponse.OnSuccessMessage)
return 0
}
return 0
}
func getRepositoryDir() (string, error) {
dir, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to fetch current working directory: %s", err)
return "", err
}
outputBytes, err := exec.Command("git", "-C", dir, "rev-parse", "--show-toplevel").CombinedOutput()
if err != nil {
if _, ok := err.(*exec.ExitError); ok {
if regexp.MustCompile("not a git repository").Match(outputBytes) {
fmt.Fprintf(os.Stderr, "The current directory is not within a Git repository.\n")
fmt.Fprintf(os.Stderr, "Please run this command from within your CodeCrafters Git repository.\n")
} else {
fmt.Fprintln(os.Stderr, string(outputBytes))
}
return "", err
} else {
panic(err)
}
}
return strings.TrimSpace(string(outputBytes)), nil
}
func copyRepositoryDirToTempDir(repoDir string) (string, error) {
tmpDir, err := ioutil.TempDir("", "codecrafters")
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create temporary directory: %s", err)
return "", err
}
err = cp.Copy(repoDir, tmpDir)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to copy to temporary directory: %s", err)
return "", err
}
return tmpDir, nil
}
func checkoutNewBranch(tempBranchName string, tmpDir string) error {
err := exec.Command("git", "-C", tmpDir, "checkout", "-b", tempBranchName).Run()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create temp branch: %s", err)
return err
}
return nil
}
func commitChanges(tmpDir string, commitMessage string) (string, error) {
outputBytes, err := exec.Command("git", "-C", tmpDir, "commit", "--allow-empty", "-a", "-m", commitMessage).CombinedOutput()
if err != nil {
if _, ok := err.(*exec.ExitError); ok {
fmt.Fprintf(os.Stderr, "failed to create temp commit: %s", outputBytes)
return "", err
} else {
fmt.Fprintf(os.Stderr, "failed to create temp commit: %s", err)
return "", err
}
}
outputBytes, err = exec.Command("git", "-C", tmpDir, "rev-parse", "HEAD").CombinedOutput()
if err != nil {
if _, ok := err.(*exec.ExitError); ok {
fmt.Fprintf(os.Stderr, "failed to fetch temp commit sha: %s", outputBytes)
return "", err
} else {
fmt.Fprintf(os.Stderr, "failed to fetch temp commit sha: %s", err)
return "", err
}
}
return strings.TrimSpace(string(outputBytes)), nil
}
func pushBranchToRemote(tmpDir string) error {
// TODO: Find CodeCrafters remote and use that
outputBytes, err := exec.Command("git", "-C", tmpDir, "push", "origin", "HEAD").CombinedOutput()
if err != nil {
if _, ok := err.(*exec.ExitError); ok {
fmt.Fprintf(os.Stderr, "failed to push temp branch: %s", outputBytes)
return err
} else {
fmt.Fprintf(os.Stderr, "failed to push temp branch: %s", err)
return err
}
}
return nil
}
func streamLogs(logstreamUrl string) error {
consumer, err := logstream_consumer.NewConsumer(logstreamUrl, func(message string) {})
if err != nil {
fmt.Printf("Err: %v\n", err)
return err
}
_, err = io.Copy(os.Stdout, consumer)
if err != nil {
fmt.Printf("Err: %v\n", err)
return err
}
return nil
}