diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 9dbd932..83babef 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -24,7 +24,7 @@ jobs: steps: - name: Set up Go - uses: actions/setup-go@v3 + uses: actions/setup-go@v4 with: go-version: ${{ matrix.go }} id: go @@ -37,6 +37,8 @@ jobs: echo $PATH make clean make test + env: + TZ: Asia/Tokyo - name: create test run: | diff --git a/export_test.go b/export_test.go index c9ef29c..4b903ad 100644 --- a/export_test.go +++ b/export_test.go @@ -6,3 +6,6 @@ var ( LoadZipArchive = loadZipArchive MergeTags = mergeTags ) + +type VersionsOutput = versionsOutput +type VersionsOutputs = versionsOutputs diff --git a/go.mod b/go.mod index 5c25d00..13ca71e 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/fujiwara/logutils v1.1.0 github.com/fujiwara/tfstate-lookup v1.1.2 github.com/go-test/deep v1.0.7 + github.com/google/go-cmp v0.5.9 github.com/google/go-jsonnet v0.17.0 github.com/hashicorp/go-envparse v0.0.0-20200406174449-d9cfd743a15e github.com/kayac/go-config v0.6.0 diff --git a/versions.go b/versions.go index bc2a968..504fdd0 100644 --- a/versions.go +++ b/versions.go @@ -27,6 +27,7 @@ type versionsOutput struct { Version string `json:"Version"` Aliases []string `json:"Aliases,omitempty"` LastModified time.Time `json:"LastModified"` + Runtime string `json:"Runtime"` } type versionsOutputs []versionsOutput @@ -49,20 +50,26 @@ func (vo versionsOutputs) TSV() string { func (vo versionsOutputs) Table() string { buf := new(strings.Builder) w := tablewriter.NewWriter(buf) - w.SetHeader([]string{"Version", "Last Modified", "Aliases"}) + w.SetHeader([]string{"Version", "Last Modified", "Aliases", "Runtime"}) for _, v := range vo { - w.Append([]string{v.Version, v.LastModified.Local().Format(time.RFC3339), strings.Join(v.Aliases, ",")}) + w.Append([]string{ + v.Version, + v.LastModified.Local().Format(time.RFC3339), + strings.Join(v.Aliases, ","), + v.Runtime, + }) } w.Render() return buf.String() } func (v versionsOutput) TSV() string { - return fmt.Sprintf("%s\t%s\t%s\n", + return strings.Join([]string{ v.Version, v.LastModified.Local().Format(time.RFC3339), strings.Join(v.Aliases, ","), - ) + v.Runtime, + }, "\t") + "\n" } // Versions manages the versions of a Lambda function @@ -130,6 +137,7 @@ func (app *App) Versions(opt VersionsOption) error { Version: *v.Version, Aliases: aliases[*v.Version], LastModified: lm, + Runtime: string(v.Runtime), }) } diff --git a/versions_test.go b/versions_test.go new file mode 100644 index 0000000..72c422b --- /dev/null +++ b/versions_test.go @@ -0,0 +1,61 @@ +package lambroll_test + +import ( + "encoding/json" + "testing" + "time" + + "github.com/fujiwara/lambroll" + "github.com/google/go-cmp/cmp" +) + +// TestFixedTime is a fixed time for testing (JST) +var TestFixedTime = time.Date(2023, 8, 30, 12, 34, 56, 0, time.FixedZone("Asia/Tokyo", 9*60*60)) + +var TestVersionsOutputs = lambroll.VersionsOutputs{ + {Version: "1", LastModified: TestFixedTime, Runtime: "go1.x"}, + {Version: "2", LastModified: TestFixedTime, Runtime: "python3.8", Aliases: []string{"current", "latest"}}, +} + +func TestVersionsJSON(t *testing.T) { + jsonOutput := TestVersionsOutputs.JSON() + + var parsedOutputs lambroll.VersionsOutputs + err := json.Unmarshal([]byte(jsonOutput), &parsedOutputs) + + if err != nil { + t.Errorf("Failed to unmarshal JSON: %s", err) + } + + if d := cmp.Diff(parsedOutputs, TestVersionsOutputs); d != "" { + t.Errorf("JSON mismatch: diff:%s", d) + } +} + +func TestVersionsTSV(t *testing.T) { + t.Setenv("TZ", "UTC+9") + expectedTSV := "1\t2023-08-30T12:34:56+09:00\t\tgo1.x\n" + + "2\t2023-08-30T12:34:56+09:00\tcurrent,latest\tpython3.8\n" + + if d := cmp.Diff(TestVersionsOutputs.TSV(), expectedTSV); d != "" { + t.Errorf("TSV mismatch: diff:%s", d) + } +} + +func TestVersionsTable(t *testing.T) { + t.Setenv("TZ", "UTC+9") + tableOutput := TestVersionsOutputs.Table() + expectedOutput := ` ++---------+---------------------------+----------------+-----------+ +| VERSION | LAST MODIFIED | ALIASES | RUNTIME | ++---------+---------------------------+----------------+-----------+ +| 1 | 2023-08-30T12:34:56+09:00 | | go1.x | +| 2 | 2023-08-30T12:34:56+09:00 | current,latest | python3.8 | ++---------+---------------------------+----------------+-----------+ +` + expectedOutput = expectedOutput[1:] // remove first newline + + if d := cmp.Diff(tableOutput, expectedOutput); d != "" { + t.Errorf("Table mismatch: diff:%s", d) + } +}