GitLab Duo Principles - Hands-On Lab: Using GitLab Duo to Write New Code

This Hands-On Guide walks you through using GitLab Duo to generate code and tests.

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

  1. Navigate to the root of your GitLab Duo Principles Project.

  2. Select the main.go file.

  3. Select Edit > Open in Web IDE.

  4. Between the import block and main 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
    
  5. Press enter and note the small Tanuki icon that appears in the code margin. Wait until code appears. Once you see code, press Tab.

  6. Call your new function in the main() function as part of the new myFigure object. Make sure you provide an index as an argument. In the example code, the index provided is 0.

  7. 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!

  1. Highlight all of the code in the func print_phrase() string function.

  2. Select the Tanuki icon in the left sidebar.

  3. 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.

  4. To add the tests to your project, start by copying the code generated by GitLab Duo.

  5. From the left sidebar, select the Explorer icon.

  6. To add the tests to your project, create a new file named print_phrases.test.go.

  7. 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"
    
  8. Below the import for testing, on a new line, paste the generated tests from GitLab Duo.

  9. From the left sidebar, select Source Control, enter any commit message, and select Commit and push to main.

  10. Select Go to Project to return to your project.

Task C. Run tests in a CI/CD pipeline

  1. Navigate to Build > Pipeline editor.

  2. In the stages section, add a new stage called test.

  3. Under the build app job create a new job called test.

  4. 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 ./...
    
  5. Select Commit changes.

  6. 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.

Last modified November 1, 2024: Remove trailing spaces (6f6d0996)