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 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)
}
}