#Is it possible to write golang test for a function

1 messages · Page 1 of 1 (latest)

split mesa
#

I am trying to write a golang test for a dagger function, but I get: panic: DAGGER_SESSION_PORT is not set.

Maybe it is not possible, du you know, and if so how - thanks.

Code:

package main

import (
"context"

"dagger/dockerfile/internal/dagger"

)

type Dockerfile struct{}

// Build and publish image from existing Dockerfile
func (m *Dockerfile) Build(
ctx context.Context,
// location of directory containing Dockerfile
src *dagger.Directory,
) (string, error) {
ref, err := dag.Container().
WithDirectory("/src", src).
WithWorkdir("/src").
Directory("/src").
DockerBuild(). // build from Dockerfile
Publish(ctx, "ttl.sh/health:1h")

if err != nil {
    return "", err
}

return ref, nil

}

func TestMain(T *testing.T) {
doc := Dockerfile{}
dir := dag.Directory().Directory("/src")
doc.Build(context.Background(), dir)
}

wicked kestrel
#

Hi @split mesa!! Unit testing module functions is not possible at the moment. Dagger functions need to be executed in a special context that the dagger CLI (and the engine) take care of. What I often do when trying to unit test is create another module that installs my main module and uses it. Then I do a dagger call to my testing module to make sure things work as they should