ajhahn.de
← eeco
Go 85 lines
package workflow

import (
	"os"
	"path/filepath"
	"testing"
)

func TestWorkflowExists_DetectsScaffoldedDir(t *testing.T) {
	cfg := newCfg(t)
	if WorkflowExists(cfg, "missing") {
		t.Error("missing workflow reported as existing")
	}
	dir := filepath.Join(cfg.Workspace, "workflows", "present")
	if err := os.MkdirAll(dir, 0o755); err != nil {
		t.Fatal(err)
	}
	if !WorkflowExists(cfg, "present") {
		t.Error("scaffolded workflow reported as missing")
	}
}

func TestDisableEnableRoundTrip(t *testing.T) {
	cfg := newCfg(t)
	dir := filepath.Join(cfg.Workspace, "workflows", "wf")
	if err := os.MkdirAll(dir, 0o755); err != nil {
		t.Fatal(err)
	}
	if IsDisabled(cfg, "wf") {
		t.Fatal("fresh workflow should not be disabled")
	}
	if err := Disable(cfg, "wf"); err != nil {
		t.Fatalf("Disable: %v", err)
	}
	if !IsDisabled(cfg, "wf") {
		t.Error("Disable did not flip IsDisabled")
	}
	if _, err := os.Stat(filepath.Join(dir, DisabledMarker)); err != nil {
		t.Errorf("marker file missing after Disable: %v", err)
	}
	// Repeat Disable: clean no-op.
	if err := Disable(cfg, "wf"); err != nil {
		t.Errorf("repeat Disable returned error: %v", err)
	}
	if err := Enable(cfg, "wf"); err != nil {
		t.Fatalf("Enable: %v", err)
	}
	if IsDisabled(cfg, "wf") {
		t.Error("Enable did not flip IsDisabled")
	}
	// Repeat Enable on an already-enabled workflow: clean no-op.
	if err := Enable(cfg, "wf"); err != nil {
		t.Errorf("repeat Enable returned error: %v", err)
	}
}

func TestDisableEnable_RejectMissingWorkflow(t *testing.T) {
	cfg := newCfg(t)
	if err := Disable(cfg, "ghost"); err == nil {
		t.Error("Disable should reject a missing workflow")
	}
	if err := Enable(cfg, "ghost"); err == nil {
		t.Error("Enable should reject a missing workflow")
	}
}

func TestDisableEnable_RejectBadName(t *testing.T) {
	cfg := newCfg(t)
	if err := Disable(cfg, "Bad/Name"); err == nil {
		t.Error("Disable should reject a bad name")
	}
	if err := Enable(cfg, "Bad/Name"); err == nil {
		t.Error("Enable should reject a bad name")
	}
}

func TestDisableEnable_RejectNilConfig(t *testing.T) {
	if err := Disable(nil, "wf"); err == nil {
		t.Error("Disable should reject nil config")
	}
	if err := Enable(nil, "wf"); err == nil {
		t.Error("Enable should reject nil config")
	}
}