From 4c45a289f7fcccae5904223935bc640cd58e44e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Hedenstro=CC=88m?= <erik@hedenstroem.com> Date: Tue, 21 May 2024 01:35:30 +0200 Subject: [PATCH] Added support for rendering dot files --- snok_test.go | 3 +++ test_graph.go | 15 +++++++++++++++ test_suite.go | 14 ++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/snok_test.go b/snok_test.go index c756f96..400c089 100644 --- a/snok_test.go +++ b/snok_test.go @@ -44,6 +44,9 @@ func TestCmds(t *testing.T) { testcontainers.Logger = log.New(os.Stdout, " ", 0) } suite.Run(t) + if suite.GetLogLevel() >= DebugLevel { + t.Log(suite.Graph) + } } func mockContainer(s *TestSuite, t *testing.T, args []string) { diff --git a/test_graph.go b/test_graph.go index 68010b5..005a4f3 100644 --- a/test_graph.go +++ b/test_graph.go @@ -20,6 +20,10 @@ func NewTestGraph() *TestGraph { return &TestGraph{simple.NewDirectedGraph()} } +func (g *TestGraph) String() string { + return g.Dot() +} + func (g *TestGraph) AddJob(job graph.Node) { g.Graph.AddNode(job) } @@ -86,3 +90,14 @@ func (g *TestGraph) ResolveDeps() error { func (g *TestGraph) SortJobs() ([]graph.Node, error) { return topo.Sort(g.Graph) } + +func (g *TestGraph) Dot() string { + dot := "digraph G {\n" + for _, node := range graph.NodesOf(g.Graph.Nodes()) { + dot = dot + fmt.Sprintf(" %q;\n", node) + } + for _, edge := range graph.EdgesOf(g.Graph.Edges()) { + dot = dot + fmt.Sprintf(" %q -> %q;\n", edge.From(), edge.To()) + } + return dot + "}\n" +} diff --git a/test_suite.go b/test_suite.go index c41bd9c..0e5fa88 100644 --- a/test_suite.go +++ b/test_suite.go @@ -25,6 +25,7 @@ type TestSuite struct { RootCmd *cobra.Command LogLevel LogLevel Graph *TestGraph + dotFile string unexpectedErr func(require.TestingT, error, ...interface{}) } @@ -38,6 +39,7 @@ func NewTestSuite(cmd *cobra.Command) *TestSuite { } var l string flag.StringVar(&l, "l", "info", "Log level (error, warn, info, debug, trace)") + flag.StringVar(&s.dotFile, "dot", "", "Output dot file") flag.Parse() if os.Getenv("SNOK_LOG_LEVEL") != "" { l = os.Getenv("SNOK_LOG_LEVEL") @@ -115,6 +117,18 @@ func (s *TestSuite) Run(t *testing.T) { s.addTests(t, s.RootCmd, []string{}) err := s.Graph.ResolveDeps() require.NoError(t, err) + if s.dotFile != "" { + f, err := os.Create(s.dotFile) + require.NoError(t, err) + n, err := f.WriteString(s.Graph.Dot()) + require.NoError(t, err) + t.Logf("Wrote %d bytes to %s", n, s.dotFile) + } else { + s.runTests(t) + } +} + +func (s *TestSuite) runTests(t *testing.T) { jobs, err := s.Graph.SortJobs() require.NoError(t, err) for _, job := range jobs { -- GitLab