package dlog import ( "fmt" "os" "path/filepath" "testing" "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "edge-infra.dev/pkg/lib/build/bazel" ) func TestFromMarkdown(t *testing.T) { tcs := []struct { file string expected *Decision }{ { "testdata/0001-proposed.md", &Decision{ Status: "proposed", Date: Time{time.Date(2017, 01, 11, 0, 0, 0, 0, time.UTC)}, Deciders: []string{"foo", "barry"}, Tags: []string{"invalid", "welp", "oh no"}, file: "0001-proposed.md", number: "0001", title: "My Decision", }, }, { "testdata/0002-rejected.md", &Decision{ Status: "rejected", Date: Time{time.Date(1997, 06, 19, 0, 0, 0, 0, time.UTC)}, Deciders: []string{"@as130521", "@ncrvoyix-swt-retail/edge"}, Informed: []string{"@nobody"}, Consulted: []string{"@nobody"}, Tags: []string{"build", "ci", "test"}, file: "0002-rejected.md", number: "0002", title: "Use Argo Workflows for K8s automation engine", }, }, { "testdata/0003-accepted.md", &Decision{ Status: "accepted", Date: Time{time.Date(2033, 03, 30, 0, 0, 0, 0, time.UTC)}, Deciders: []string{"@swim", "@ncrvoyix-swt-retail/edge-foundation"}, file: "0003-accepted.md", number: "0003", title: "My Decision", }, }, } for i, tc := range tcs { tc := tc t.Run(fmt.Sprintf("%s-%d", t.Name(), i), func(t *testing.T) { d, err := FromMarkdown(tc.file) assert.NoError(t, err) assert.NotNil(t, d) assert.Equal(t, tc.expected, d) }) } } func TestToMarkdown(t *testing.T) { tcs := []struct { file string fn func(*Decision) }{ { "testdata/0001-proposed.md", func(d *Decision) { d.Tags = d.Tags[:len(d.Tags)-1] }, }, { "testdata/0003-accepted.md", func(d *Decision) { d.Status = "rejected" d.Date.Time = time.Date(2033, 10, 29, 0, 0, 0, 0, time.UTC) d.Tags = []string{"build", "ci", "automation"} }, }, } dir, err := bazel.NewTestTmpDir("dlog-tomarkdown-*") require.NoError(t, err) for i, tc := range tcs { tc := tc t.Run(fmt.Sprintf("%s-%d", t.Name(), i), func(t *testing.T) { // Copy source file to testdir so that the edits we produce contain the // original non-frontmatter content. orig, err := os.ReadFile(tc.file) require.NoError(t, err) dlogPath := filepath.Join(dir, filepath.Base(tc.file)) require.NoError(t, os.WriteFile(dlogPath, orig, 0644)) d, err := FromMarkdown(tc.file) require.NoError(t, err) require.NotNil(t, d) tc.fn(d) assert.NoError(t, ToMarkdown(d, dir)) exp, err := os.ReadFile(fmt.Sprintf("testdata/tomarkdown/expected-%d.md", i)) require.NoError(t, err) actual, err := os.ReadFile(dlogPath) require.NoError(t, err) assert.Equal(t, string(exp), string(actual)) }) } } func TestFileName(t *testing.T) { tcs := []struct { name string valid bool }{ {"001-valid.md", true}, {"0001-valid.md", true}, {"000001-valid.md", true}, {"1-number-too-short.md", false}, {"01-number-too-short.md", false}, {"0001-extra--dash.md", false}, {"no-number.md", false}, {"0001-ending-dash-.md", false}, {"0193-special-!-characters@.md", false}, } for i, tc := range tcs { t.Run(fmt.Sprintf("%s-%d", t.Name(), i), func(t *testing.T) { err := IsValidFileName(tc.name) switch { case err != nil && tc.valid: t.Error("unexpected error", err) case err == nil && !tc.valid: t.Error("expected an error, got none") } }) } }