mirror of
https://gitlab.com/TECHNOFAB/nixtest.git
synced 2025-12-12 02:00:18 +01:00
refactor: split into packages and add tests
This commit is contained in:
parent
fd58344ca7
commit
11117e0c0e
28 changed files with 2736 additions and 636 deletions
82
internal/errors/errors.go
Normal file
82
internal/errors/errors.go
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
package errors
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// NixBuildError indicates an error during `nix build`
|
||||
type NixBuildError struct {
|
||||
Derivation string
|
||||
Stderr string
|
||||
Err error // underlying error from exec.Cmd or similar
|
||||
}
|
||||
|
||||
func (e *NixBuildError) Error() string {
|
||||
return fmt.Sprintf("nix build for %s failed: %v (stderr: %s)", e.Derivation, e.Err, e.Stderr)
|
||||
}
|
||||
func (e *NixBuildError) Unwrap() error { return e.Err }
|
||||
|
||||
// NixNoOutputPathError indicates `nix build` succeeded but produced no output path
|
||||
type NixNoOutputPathError struct {
|
||||
Derivation string
|
||||
Stderr string
|
||||
}
|
||||
|
||||
func (e *NixNoOutputPathError) Error() string {
|
||||
return fmt.Sprintf("nix build for %s produced no output path (stderr: %s)", e.Derivation, e.Stderr)
|
||||
}
|
||||
|
||||
// FileReadError indicates an error reading a file, often a derivation output
|
||||
type FileReadError struct {
|
||||
Path string
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e *FileReadError) Error() string {
|
||||
return fmt.Sprintf("failed to read file %s: %v", e.Path, e.Err)
|
||||
}
|
||||
func (e *FileReadError) Unwrap() error { return e.Err }
|
||||
|
||||
// JSONUnmarshalError indicates an error unmarshalling JSON data
|
||||
type JSONUnmarshalError struct {
|
||||
Source string // e.g. file path or "derivation output"
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e *JSONUnmarshalError) Error() string {
|
||||
return fmt.Sprintf("failed to unmarshal JSON from %s: %v", e.Source, e.Err)
|
||||
}
|
||||
func (e *JSONUnmarshalError) Unwrap() error { return e.Err }
|
||||
|
||||
// ScriptExecutionError indicates an error starting or waiting for a script
|
||||
type ScriptExecutionError struct {
|
||||
Path string // path to script that was attempted to run
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e *ScriptExecutionError) Error() string {
|
||||
return fmt.Sprintf("script %s execution failed: %v", e.Path, e.Err)
|
||||
}
|
||||
func (e *ScriptExecutionError) Unwrap() error { return e.Err }
|
||||
|
||||
// SnapshotCreateError indicates an error during snapshot creation
|
||||
type SnapshotCreateError struct {
|
||||
FilePath string
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e *SnapshotCreateError) Error() string {
|
||||
return fmt.Sprintf("failed to create/update snapshot %s: %v", e.FilePath, e.Err)
|
||||
}
|
||||
func (e *SnapshotCreateError) Unwrap() error { return e.Err }
|
||||
|
||||
// SnapshotLoadError indicates an error loading a snapshot file
|
||||
type SnapshotLoadError struct {
|
||||
FilePath string
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e *SnapshotLoadError) Error() string {
|
||||
return fmt.Sprintf("failed to load/parse snapshot %s: %v", e.FilePath, e.Err)
|
||||
}
|
||||
func (e *SnapshotLoadError) Unwrap() error { return e.Err }
|
||||
112
internal/errors/errors_test.go
Normal file
112
internal/errors/errors_test.go
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
package errors
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNixBuildError(t *testing.T) {
|
||||
underlyingErr := errors.New("exec: \"nix\": executable file not found in $PATH")
|
||||
err := &NixBuildError{
|
||||
Derivation: "test.drv",
|
||||
Stderr: "some stderr output",
|
||||
Err: underlyingErr,
|
||||
}
|
||||
|
||||
expectedMsg := "nix build for test.drv failed: exec: \"nix\": executable file not found in $PATH (stderr: some stderr output)"
|
||||
if err.Error() != expectedMsg {
|
||||
t.Errorf("Error() got %q, want %q", err.Error(), expectedMsg)
|
||||
}
|
||||
|
||||
if !errors.Is(err, underlyingErr) {
|
||||
t.Errorf("Unwrap() failed, underlying error not found")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNixNoOutputPathError(t *testing.T) {
|
||||
err := &NixNoOutputPathError{
|
||||
Derivation: "empty.drv",
|
||||
Stderr: "build successful, but no paths",
|
||||
}
|
||||
expectedMsg := "nix build for empty.drv produced no output path (stderr: build successful, but no paths)"
|
||||
if err.Error() != expectedMsg {
|
||||
t.Errorf("Error() got %q, want %q", err.Error(), expectedMsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileReadError(t *testing.T) {
|
||||
underlyingErr := os.ErrPermission
|
||||
err := &FileReadError{
|
||||
Path: "/tmp/file.json",
|
||||
Err: underlyingErr,
|
||||
}
|
||||
expectedMsg := "failed to read file /tmp/file.json: permission denied"
|
||||
if err.Error() != expectedMsg {
|
||||
t.Errorf("Error() got %q, want %q", err.Error(), expectedMsg)
|
||||
}
|
||||
if !errors.Is(err, underlyingErr) {
|
||||
t.Errorf("Unwrap() failed, underlying error not found")
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONUnmarshalError(t *testing.T) {
|
||||
underlyingErr := errors.New("unexpected end of JSON input")
|
||||
err := &JSONUnmarshalError{
|
||||
Source: "/tmp/data.json",
|
||||
Err: underlyingErr,
|
||||
}
|
||||
expectedMsg := "failed to unmarshal JSON from /tmp/data.json: unexpected end of JSON input"
|
||||
if err.Error() != expectedMsg {
|
||||
t.Errorf("Error() got %q, want %q", err.Error(), expectedMsg)
|
||||
}
|
||||
if !errors.Is(err, underlyingErr) {
|
||||
t.Errorf("Unwrap() failed, underlying error not found")
|
||||
}
|
||||
}
|
||||
|
||||
func TestScriptExecutionError(t *testing.T) {
|
||||
underlyingErr := errors.New("command timed out")
|
||||
err := &ScriptExecutionError{
|
||||
Path: "/tmp/script.sh",
|
||||
Err: underlyingErr,
|
||||
}
|
||||
expectedMsg := "script /tmp/script.sh execution failed: command timed out"
|
||||
if err.Error() != expectedMsg {
|
||||
t.Errorf("Error() got %q, want %q", err.Error(), expectedMsg)
|
||||
}
|
||||
if !errors.Is(err, underlyingErr) {
|
||||
t.Errorf("Unwrap() failed, underlying error not found")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotCreateError(t *testing.T) {
|
||||
underlyingErr := os.ErrExist
|
||||
err := &SnapshotCreateError{
|
||||
FilePath: "/snapshots/test.snap.json",
|
||||
Err: underlyingErr,
|
||||
}
|
||||
expectedMsg := "failed to create/update snapshot /snapshots/test.snap.json: file already exists"
|
||||
if err.Error() != expectedMsg {
|
||||
t.Errorf("Error() got %q, want %q", err.Error(), expectedMsg)
|
||||
}
|
||||
if !errors.Is(err, underlyingErr) {
|
||||
t.Errorf("Unwrap() failed, underlying error not found")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotLoadError(t *testing.T) {
|
||||
underlyingErr := &JSONUnmarshalError{Source: "test.snap.json", Err: fmt.Errorf("bad json")}
|
||||
err := &SnapshotLoadError{
|
||||
FilePath: "/snapshots/test.snap.json",
|
||||
Err: underlyingErr,
|
||||
}
|
||||
expectedMsg := "failed to load/parse snapshot /snapshots/test.snap.json: failed to unmarshal JSON from test.snap.json: bad json"
|
||||
if err.Error() != expectedMsg {
|
||||
t.Errorf("Error() got %q, want %q", err.Error(), expectedMsg)
|
||||
}
|
||||
if !errors.Is(err, underlyingErr) {
|
||||
t.Errorf("Unwrap() failed, underlying error not found")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue