You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -63,142 +63,91 @@ Evaluation script is actually what runs when the user on the codedamn playground
63
63
64
64

65
65
66
-
Since we have already written a pure evaluation script that runs and finally writes the JSON result to the `UNIT_TEST_OUTPUT_FILE` environment, all we have to do is trigger that script via Node.js.
67
-
68
-
The full path of the script is made available at run-time with another environment variable called `TEST_FILE_NAME`. Therefore, all we have to do is write the following in the evaluation script area:
66
+
We have to write a boolean array to the file `$UNIT_TEST_OUTPUT_FILE`. Since go already has a builtin testing utility, we can use that as follows:
69
67
70
68
```sh
71
-
node $TEST_FILE_NAME
72
-
```
69
+
#!/bin/bash
70
+
set -e 1
73
71
74
-
This will make sure we run the full Node.js script and write the results properly for the playground IDE to read. It would look like the following:
go mod init codedamn # assuming you used "codedamn" as the package
77
+
go test -json -parallel 1 > codedamn_evaluation_output.json
77
78
78
-
**Note:** You can setup a full testing environment in this block of evaluation script (installing more packages, etc. if you want). However, your test file will be timed out **after 30 seconds**. Therefore, make sure, all of your testing can happen within 30 seconds.
79
+
# process results file
80
+
cat > processGoResults.js <<EOF
81
+
const fs = require('fs')
79
82
80
-
## Step 5 - Test file
83
+
// Read the test results file into memory as a string
When you click on it, a new window will open. This is a test file area.
96
+
const passed = output === 'PASS'
87
97
88
-
You can write anything here. Whatever script you write here, can be executed from the `Test command to run section` inside the evaluation tab we were in earlier.
98
+
// Add the pass/fail status to the array
99
+
results.push(passed)
100
+
})
89
101
90
-
The point of having a file like this to provide you with a place where you can write your evaluation script.
- We copy our test script (that we will write in next step) in the same directory as user code `/home/damner/code`. This way we can access user packages comfortably.
116
+
- We run `go mod init` since `go.mod` file is required to be present when you run `go test` utility. If you have already created a `go.mod` file (in the default repository file setup), this command would do nothing
117
+
- We now run `go test` with `-json` and `-parallel 1` flag. We need `json` flag as we will parse it using a simple Node.js script written later. You can use Go for that parsing too (if you can write an equivalent). We need `-parallel 1` so that we process the tests in correct order (since the mapping of the boolean array here is linked to how tests would appear on frontend).
118
+
- Go's test util output is stored in a file called `codedamn_evaluation_output.json`. This file technically is not a valid JSON since Go streams all output as individual JSON objects.
119
+
- We finally run the Node.js script that splits the file `codedamn_evaluation_output.json` by newline. We parse individual lines as JSON and only process the objects where the `.Output` field in JSON is either `PASS` or `FAIL`.
120
+
- Since we have `-parallel 1`, the output order would be preserved on how you wrote the tests.
121
+
- We store this boolean array into the `$UNIT_TEST_OUTPUT_FILE` which is then visible to the end user.
181
122
182
-
Let us understand what is happening here exactly:
123
+
**Note:** Be careful with the package imports in go, package name and test file location.
183
124
184
-
- Remember that we can code anything in this file and then execute it later. In this example, we're writing a Node.js script from scratch.
185
-
- Remember that we already have puppeteer with headless chrome pre-installed in codedamn playgrounds for HTML/CSS. Therefore, we can import it directly.
186
-
- In the first part of `run` function, we start a headless puppeteer browser.
187
-
- We then visit `http://localhost:1337`. At this point, I would highly recommend you to read [How port mapping works for codedamn playgrounds](/docs/concepts/port-mapping), if you haven't yet.
188
-
- From this point onwards, we have some `try-catch` blocks. But why? Because we want to populate an array `results` and then finally write this array to a file inside environment variable `UNIT_TEST_OUTPUT_FILE`
189
-
- Let's say, `[true, false]` is written to the file `process.env.UNIT_TEST_OUTPUT_FILE`. In that case, the first challenge would be marked as passed in the IDE, and the second challenge would be marked as failed:
125
+
## Step 5 - Test file
190
126
191
-

127
+
You will see a button named `Edit Test File` in the `Evaluation` tab. Click on it.
192
128
193
-
- Whatever your mapping of final JSON boolean array written in `process.env.UNIT_TEST_OUTPUT_FILE` is, it is matched exactly to the results on the playground. For example, if the array written is `[true, false, true, true]`, the following would be the output on playground:
129
+

194
130
195
-

131
+
When you click on it, a new window will open. This is a test file area.
196
132
197
-
-**Note:** If your `results` array contain less values than challenges added back in the UI, the "extra" UI challenges would automatically stay as "false". If you add more challenges in test file, the results would be ignored. Therefore, it is **important** that the `results.length` is same as the number of challenges you added in the challenges UI.
133
+
You can write anything here. Whatever script you write here, can be executed from the `Test command to run section` inside the evaluation tab we were in earlier.
198
134
199
-
- We then also add jQuery and chai for assisting with testing. Although it is not required as long as you can populate the `results` array properly.
135
+
The point of having a file like this to provide you with a place where you can write your evaluation script. As we decided earlier, you can write a simple Go test here:
136
+
137
+
```go
138
+
package codedamn
139
+
140
+
import"testing"
141
+
142
+
funcTestAdd(t *testing.T) {
143
+
got:=add(2, 3)
144
+
if got != 5 {
145
+
t.Errorf("add(2, 3) = %d; want 5", got)
146
+
}
147
+
}
148
+
```
200
149
201
-
This completes your evaluation script for the lab. Your lab is now almost ready for users.
150
+
This file contains only one test at the moment that would give JSON output stream that will be processed further by our Node.js script earlier. This completes your evaluation script for the lab. Your lab is now almost ready for users.
0 commit comments