Skip to main content

Testing and building a Go project

This guide explains how to run some tests and build a Go project with the go cue package.

Plan

The plan consist of 2 actions:

  • test for go test
  • build for go build

both use the Go dagger package

package main

import (
"dagger.io/dagger"

"universe.dagger.io/go"
)

dagger.#Plan & {
client: filesystem: "./hello": read: contents: dagger.#FS

actions: {
test: go.#Test & {
source: client.filesystem."./hello".read.contents
packages: ["./..."]
}

build: go.#Build & {
source: client.filesystem."./hello".read.contents
}
}

}

Go project

The project is a simple program that takes $NAME environment variable, use the Go greeting package to print the greeting.Greeting() out.

hello/main.go
package main

import (
"fmt"
"os"

"dagger.io/testgreetci/greeting"
)

func main() {
name := os.Getenv("NAME")
if name == "" {
name = "John Doe"
}
fmt.Printf(greeting.Greeting(name))
}
hello/greeting/greeting.go
package greeting

import "fmt"

func Greeting(name string) string {
return fmt.Sprintf("Hi %s!", name)
}
hello/greeting/greeting_test.go
package greeting

import "testing"

func TestGreeting(t *testing.T) {
name := "Dagger Test"
expect := "Hi Dagger Test!"
value := Greeting(name)

if expect != value {
t.Fatalf("Hello(%s) = '%s', expected '%s'", name, value, expect)
}
}

Dagger Cloud

Dagger Cloud is a managed service that aims to help you troubleshoot your current dagger runs by storing operation history and enabling functionality such as storing outputs and a comprehensive detailed view of your executions.

If you're interested in trying out Dagger Cloud, you can find more information in our docs section. Furthermore, if you have any feedback or ideas that could help improve the product, there is an open discussion in GitHub where you can leave us your inputs.