Skip to content
Merged
Prev Previous commit
Next Next commit
add -update flag to update goldens
  • Loading branch information
Emyrk committed Apr 11, 2024
commit 72d87887f8cffd1d85a75e9dee680744610eef83
13 changes: 10 additions & 3 deletions scripts/apitypings/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package main

import (
"fmt"
"flag"
"os"
"path/filepath"
"strings"
Expand All @@ -16,6 +16,9 @@ import (
"github.com/stretchr/testify/require"
)

// updateGoldenFiles is a flag that can be set to update golden files.
var updateGoldenFiles = flag.Bool("update", false, "Update golden files")

func TestGeneration(t *testing.T) {
t.Parallel()
files, err := os.ReadDir("testdata")
Expand All @@ -38,8 +41,12 @@ func TestGeneration(t *testing.T) {
require.NoErrorf(t, err, "read file %s", golden)
expectedString := strings.TrimSpace(string(expected))
output = strings.TrimSpace(output)
fmt.Println(string(output))
require.Equal(t, expectedString, output, "matched output")
if *updateGoldenFiles {
err := os.WriteFile(golden, []byte(output), 0644)
require.NoError(t, err, "write golden file")
} else {
require.Equal(t, expectedString, output, "matched output")
}
})
}
}
4 changes: 2 additions & 2 deletions scripts/apitypings/testdata/enums/enums.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package codersdk

type (
Enum string
Enums []Enum
Enum string
EnumSliceType []Enum
)

const (
Expand Down
2 changes: 1 addition & 1 deletion scripts/apitypings/testdata/enums/enums.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// From codersdk/enums.go
export type Enums = readonly Enum[]
export type EnumSliceType = (readonly Enum[])

// From codersdk/enums.go
export type Enum = "bar" | "baz" | "foo" | "qux"
Expand Down
12 changes: 6 additions & 6 deletions scripts/apitypings/testdata/genericmap/genericmap.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package codersdk

type Foo struct {
Bar string `json:"bar"`
}

type Buzz struct {
Foo `json:"foo"`
Bazz string `json:"bazz"`
}

type Custom interface {
Foo | Buzz
type Foo struct {
Bar string `json:"bar"`
}

type FooBuzz[R Custom] struct {
Something []R `json:"something"`
}

type Custom interface {
Foo | Buzz
}

// Not yet supported
//type FooBuzzMap[R Custom] struct {
// Something map[string]R `json:"something"`
Expand Down
4 changes: 2 additions & 2 deletions scripts/apitypings/testdata/genericmap/genericmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export interface Foo {

// From codersdk/genericmap.go
export interface FooBuzz<R extends Custom> {
readonly something: readonly R[]
readonly something: (readonly R[])
}

// From codersdk/genericmap.go
export type Custom = Foo | Buzz
export type Custom = Foo | Buzz
4 changes: 2 additions & 2 deletions scripts/apitypings/testdata/generics/generics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export interface Static {
}

// From codersdk/generics.go
export type Custom = string | boolean | number | readonly string[] | null
export type Custom = string | boolean | number | (readonly string[]) | null

// From codersdk/generics.go
export type Single = string

export type comparable = boolean | number | string | any
export type comparable = boolean | number | string | any
8 changes: 4 additions & 4 deletions scripts/apitypings/testdata/genericslice/genericslice.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// From codersdk/genericslice.go
export interface Bar {
readonly Bar: string
readonly Bar: string
}

// From codersdk/genericslice.go
export interface Foo<R extends any> {
Copy link
Member

@Parkreiner Parkreiner Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for the future: R extends any actually doesn't do anything at all. Since any is a top type, everything in TypeScript extends any, so the type constraint provides zero protection

At some point, we'd probably want to turn this into

export interface Foo<R = unknown> {
  // Stuff
}

readonly Slice: readonly R[]
readonly TwoD: readonly (readonly R[])[]
}
readonly Slice: (readonly R[])
readonly TwoD: (readonly (readonly R[])[])
}