GitLab Duo Principles - Hands-On Lab: Using GitLab Duo to Write New Code
Estimated time to complete: 25 minutes
Objectives
In this lab, you will see how GitLab Duo can writing and testing code inside of a project.
Task A. Using Code Suggestions to Write a New Function
-
Navigate to the root of your GitLab Duo Principles Project.
-
Select the
main.go
file. -
Select Edit > Open in Web IDE.
-
Between the
import
block andmain
function, type the following comment:// write a function called print_phrase that contains a list of string values. It takes in an index of the list and returns the value at the provided index from a list as a string
-
Press enter and note the small Tanuki icon that appears in the code margin. Wait until code appears. Once you see code, press Tab.
-
Call your new function in the
main()
function as part of the newmyFigure
object. Make sure you provide an index as an argument. In the example code, the index provided is 0. -
After doing this, your code should look similar to this:
package main import ( "github.com/common-nighthawk/go-figure" ) // write a function called print_phrase that contains a list of string values. It takes in an index of the list and returns the value at the provided index from a list as a string func print_phrase(index int) string { phrases := []string{ "Hello, world!", "Go is awesome", "Programming is fun", "Keep coding", "Practice makes perfect", } if index < 0 || index >= len(phrases) { return "Invalid index" } return phrases[index] } func main() { myFigure := figure.NewFigure(print_phrase(0), "", true) myFigure.Print() }
By nature, the prompt we entered will generate different code for most users, since we asked for random words from a list. If you see different words in the
phrases :=
definition, that is ok.
Task B. Generating Code Tests
The code generated by the GitLab Duo is likely correct, however it is still best practice to test the code for errors before using it in any production environment. Luckily, GitLab Duo can also help us with the test generation process!
-
Highlight all of the code in the
func print_phrase() string
function. -
Select the Tanuki icon in the left sidebar.
-
Type the prompt
/tests
. You will receive some tests similar to below:package main import ( "testing" ) func TestPrintPhrase(t *testing.T) { tests := []struct { name string index int expected string }{ {"Valid index 0", 0, "Hello, world!"}, {"Valid index 2", 2, "Programming is fun"}, {"Valid index 4", 4, "Practice makes perfect"}, {"Negative index", -1, "Invalid index"}, {"Index out of range", 5, "Invalid index"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := print_phrase(tt.index) if result != tt.expected { t.Errorf("print_phrase(%d) = %s; want %s", tt.index, result, tt.expected) } }) } }
As always, feel free to use
/explain
to understand what this code is doing. -
To add the tests to your project, start by copying the code generated by GitLab Duo.
-
From the left sidebar, select the Explorer icon.
-
To add the tests to your project, create a new file named
print_phrases.test.go
. -
If Duo did not already add the package and testing import, include them by adding the following code to the top of the file:
package main import "testing"
-
Below the import for testing, on a new line, paste the generated tests from GitLab Duo.
-
From the left sidebar, select Source Control, enter any commit message, and select Commit and push to main.
-
Select Go to Project to return to your project.
Task C. Run tests in a CI/CD pipeline
-
Navigate to Build > Pipeline editor.
-
In the
stages
section, add a new stage calledtest
. -
Under the
build app
job create a new job calledtest
. -
Add the
go test
command to run the tests. Your.gitlab-ci.yml
file will look like the following:stages: - build - test default: image: golang:latest build app: stage: build script: - go get github.com/common-nighthawk/go-figure - go run main.go test: stage: test script: - go get github.com/common-nighthawk/go-figure - go test ./...
-
Select Commit changes.
-
Wait for the pipeline to complete and review the results.
You should now have a pipeline that passed the tests. If a job failed, feel free to use Troubleshoot to troubleshoot any errors.
Lab Guide Complete
You have completed this lab exercise. You can view the other lab guides for this course.
Suggestions?
If you’d like to suggest changes to the lab, please submit them via merge request.
6f6d0996
)