...

Source file src/edge-infra.dev/pkg/tools/dlog/decision_log_test.go

Documentation: edge-infra.dev/pkg/tools/dlog

     1  package dlog
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"edge-infra.dev/pkg/lib/build/bazel"
    14  )
    15  
    16  func TestFromMarkdown(t *testing.T) {
    17  	tcs := []struct {
    18  		file     string
    19  		expected *Decision
    20  	}{
    21  		{
    22  			"testdata/0001-proposed.md", &Decision{
    23  				Status:   "proposed",
    24  				Date:     Time{time.Date(2017, 01, 11, 0, 0, 0, 0, time.UTC)},
    25  				Deciders: []string{"foo", "barry"},
    26  				Tags:     []string{"invalid", "welp", "oh no"},
    27  				file:     "0001-proposed.md",
    28  				number:   "0001",
    29  				title:    "My Decision",
    30  			},
    31  		},
    32  		{
    33  			"testdata/0002-rejected.md", &Decision{
    34  				Status:    "rejected",
    35  				Date:      Time{time.Date(1997, 06, 19, 0, 0, 0, 0, time.UTC)},
    36  				Deciders:  []string{"@as130521", "@ncrvoyix-swt-retail/edge"},
    37  				Informed:  []string{"@nobody"},
    38  				Consulted: []string{"@nobody"},
    39  				Tags:      []string{"build", "ci", "test"},
    40  				file:      "0002-rejected.md",
    41  				number:    "0002",
    42  				title:     "Use Argo Workflows for K8s automation engine",
    43  			},
    44  		},
    45  		{
    46  			"testdata/0003-accepted.md", &Decision{
    47  				Status:   "accepted",
    48  				Date:     Time{time.Date(2033, 03, 30, 0, 0, 0, 0, time.UTC)},
    49  				Deciders: []string{"@swim", "@ncrvoyix-swt-retail/edge-foundation"},
    50  				file:     "0003-accepted.md",
    51  				number:   "0003",
    52  				title:    "My Decision",
    53  			},
    54  		},
    55  	}
    56  
    57  	for i, tc := range tcs {
    58  		tc := tc
    59  		t.Run(fmt.Sprintf("%s-%d", t.Name(), i), func(t *testing.T) {
    60  			d, err := FromMarkdown(tc.file)
    61  			assert.NoError(t, err)
    62  			assert.NotNil(t, d)
    63  
    64  			assert.Equal(t, tc.expected, d)
    65  		})
    66  	}
    67  }
    68  
    69  func TestToMarkdown(t *testing.T) {
    70  	tcs := []struct {
    71  		file string
    72  		fn   func(*Decision)
    73  	}{
    74  		{
    75  			"testdata/0001-proposed.md", func(d *Decision) {
    76  				d.Tags = d.Tags[:len(d.Tags)-1]
    77  			},
    78  		},
    79  		{
    80  			"testdata/0003-accepted.md", func(d *Decision) {
    81  				d.Status = "rejected"
    82  				d.Date.Time = time.Date(2033, 10, 29, 0, 0, 0, 0, time.UTC)
    83  				d.Tags = []string{"build", "ci", "automation"}
    84  			},
    85  		},
    86  	}
    87  
    88  	dir, err := bazel.NewTestTmpDir("dlog-tomarkdown-*")
    89  	require.NoError(t, err)
    90  	for i, tc := range tcs {
    91  		tc := tc
    92  		t.Run(fmt.Sprintf("%s-%d", t.Name(), i), func(t *testing.T) {
    93  			// Copy source file to testdir so that the edits we produce contain the
    94  			// original non-frontmatter content.
    95  			orig, err := os.ReadFile(tc.file)
    96  			require.NoError(t, err)
    97  			dlogPath := filepath.Join(dir, filepath.Base(tc.file))
    98  			require.NoError(t, os.WriteFile(dlogPath, orig, 0644))
    99  
   100  			d, err := FromMarkdown(tc.file)
   101  			require.NoError(t, err)
   102  			require.NotNil(t, d)
   103  
   104  			tc.fn(d)
   105  			assert.NoError(t, ToMarkdown(d, dir))
   106  
   107  			exp, err := os.ReadFile(fmt.Sprintf("testdata/tomarkdown/expected-%d.md", i))
   108  			require.NoError(t, err)
   109  			actual, err := os.ReadFile(dlogPath)
   110  			require.NoError(t, err)
   111  			assert.Equal(t, string(exp), string(actual))
   112  		})
   113  	}
   114  }
   115  
   116  func TestFileName(t *testing.T) {
   117  	tcs := []struct {
   118  		name  string
   119  		valid bool
   120  	}{
   121  		{"001-valid.md", true},
   122  		{"0001-valid.md", true},
   123  		{"000001-valid.md", true},
   124  		{"1-number-too-short.md", false},
   125  		{"01-number-too-short.md", false},
   126  		{"0001-extra--dash.md", false},
   127  		{"no-number.md", false},
   128  		{"0001-ending-dash-.md", false},
   129  		{"0193-special-!-characters@.md", false},
   130  	}
   131  
   132  	for i, tc := range tcs {
   133  		t.Run(fmt.Sprintf("%s-%d", t.Name(), i), func(t *testing.T) {
   134  			err := IsValidFileName(tc.name)
   135  			switch {
   136  			case err != nil && tc.valid:
   137  				t.Error("unexpected error", err)
   138  			case err == nil && !tc.valid:
   139  				t.Error("expected an error, got none")
   140  			}
   141  		})
   142  	}
   143  }
   144  

View as plain text