Quickstart
Run unit tests
The test
stage of the pipeline runs the application's unit tests and depends on the build-env
stage. Let's look at its Dagger Function next.
Inspect the Dagger Function
- Go
- Python
- TypeScript
package main
import (
"context"
"dagger/hello-dagger/internal/dagger"
)
type HelloDagger struct{}
// Return the result of running unit tests
func (m *HelloDagger) Test(ctx context.Context, source *dagger.Directory) (string, error) {
// get the build environment container
// by calling another Dagger Function
return m.BuildEnv(source).
// call the test runner
WithExec([]string{"npm", "run", "test:unit", "run"}).
// capture and return the command output
Stdout(ctx)
}
import dagger
from dagger import function, object_type
@object_type
class HelloDagger:
@function
async def test(self, source: dagger.Directory) -> str:
"""Return the result of running unit tests"""
return await (
# get the build environment container
# by calling another Dagger Function
self.build_env(source)
# call the test runner
.with_exec(["npm", "run", "test:unit", "run"])
# capture and return the command output
.stdout()
)
import { dag, Container, Directory, object, func } from "@dagger.io/dagger"
@object()
class HelloDagger {
/**
* Return the result of running unit tests
*/
@func()
async test(source: Directory): Promise<string> {
// get the build environment container
// by calling another Dagger Function
return (
this.buildEnv(source)
// call the test runner
.withExec(["npm", "run", "test:unit", "run"])
// capture and return the command output
.stdout()
)
}
}
This Dagger Function works merely as a CLI wrapper. It starts with the Container
result of the previous Dagger Function, executes the npm run test:unit run
command in the container, and captures and returns the output as a string (refer to the code comments for details).
Call the Dagger Function
Call the Dagger Function as below:
dagger call test --source=.
Here's what you should see:
All the Dagger Functions in this quickstart receive the location of the source code directory as a function argument, rather than reading it directly from the host filesystem. This is by design: Dagger Functions are fully "sandboxed" and do not have direct access to the host system. Therefore, host resources such as directories, files, environment variables, network services and so on must be explicitly passed to Dagger Functions as arguments. This "sandboxing" of Dagger Functions improves security, ensures reproducibility, and assists caching.