Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add stringer #90

Merged
merged 5 commits into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fmt:

.PHONY: test
test:
go test -exec "go run $(PWD)/cmd/codesign" -count=1 ./... -timeout 60s -v
go test -exec "go run $(PWD)/cmd/codesign" ./... -timeout 60s -v

.PHONY: download_kernel
download_kernel:
Expand All @@ -22,6 +22,10 @@ else
@mv testdata/bzImage testdata/Image
endif

.PHONY: install/stringer
install/stringer:
@go install golang.org/x/tools/cmd/stringer@latest

.PHONY: clean
clean:
@rm testdata/{Image,initramfs.cpio.gz,*.tar.gz}
76 changes: 76 additions & 0 deletions cmd/addtags/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"bytes"
"flag"
"fmt"
"go/build/constraint"
"go/format"
"os"
"os/exec"
)

var (
tags = flag.String("tags", "", "comma-separated list of build tags to apply")
file = flag.String("file", "", "file to modify")
)

func usage() {
fmt.Fprintf(os.Stderr, `
usage: addtags -tags <build tags to apply> -file FILE <subcommand args...>
`[1:])

flag.PrintDefaults()
fmt.Fprintf(os.Stderr, `
It is intended for use with 'go generate', so it also runs a subcommand,
which presumably creates the file.
Sample usage:
addtags -tags=darwin,arm64 -file linuxrosettaavailability_string.go stringer -type=LinuxRosettaAvailability
`[1:])
os.Exit(2)
}

func main() {
flag.Usage = usage
flag.Parse()
args := flag.Args()
if len(args) == 0 {
flag.Usage()
}
plusConstraint := fmt.Sprintf("// +build %s", *tags)
expr, err := constraint.Parse(plusConstraint)
check(err)
goBuildConstraint := fmt.Sprintf("//go:build %s", expr.String())

cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
check(err)
b, err := os.ReadFile(*file)
check(err)

var buf bytes.Buffer
_, err = fmt.Fprintf(&buf, "%s\n%s\n", goBuildConstraint, plusConstraint)
check(err)

_, err = buf.Write(b)
check(err)

src, err := format.Source(buf.Bytes())
check(err)

f, err := os.OpenFile(*file, os.O_TRUNC|os.O_WRONLY, 0644)
check(err)
defer f.Close()

_, err = f.Write(src)
check(err)
}

func check(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
28 changes: 28 additions & 0 deletions linuxrosettaavailability_string_arm64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions shared_directory_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
)

// LinuxRosettaAvailability represents an availability of Rosetta support for Linux binaries.
//
//go:generate go run ./cmd/addtags -tags=darwin,arm64 -file linuxrosettaavailability_string_arm64.go stringer -type=LinuxRosettaAvailability -output=linuxrosettaavailability_string_arm64.go
type LinuxRosettaAvailability int

const (
Expand Down
36 changes: 36 additions & 0 deletions shared_directory_arm64_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//go:build darwin && arm64
// +build darwin,arm64

package vz_test

import (
"testing"

"github.com/Code-Hex/vz/v2"
)

func TestLinuxRosettaAvailabilityString(t *testing.T) {
cases := []struct {
availability vz.LinuxRosettaAvailability
want string
}{
{
availability: vz.LinuxRosettaAvailabilityNotSupported,
want: "LinuxRosettaAvailabilityNotSupported",
},
{
availability: vz.LinuxRosettaAvailabilityNotInstalled,
want: "LinuxRosettaAvailabilityNotInstalled",
},
{
availability: vz.LinuxRosettaAvailabilityInstalled,
want: "LinuxRosettaAvailabilityInstalled",
},
}
for _, tc := range cases {
got := tc.availability.String()
if tc.want != got {
t.Fatalf("want %q but got %q", tc.want, got)
}
}
}
2 changes: 2 additions & 0 deletions virtualization.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func init() {
}

// VirtualMachineState represents execution state of the virtual machine.
//
//go:generate stringer -type=VirtualMachineState
type VirtualMachineState int

const (
Expand Down
46 changes: 46 additions & 0 deletions virtualization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,49 @@ func TestStop(t *testing.T) {
waitState(t, timeout, vm, vz.VirtualMachineStateStopping)
waitState(t, timeout, vm, vz.VirtualMachineStateStopped)
}

func TestVirtualMachineStateString(t *testing.T) {
cases := []struct {
state vz.VirtualMachineState
want string
}{
{
state: vz.VirtualMachineStateStopped,
want: "VirtualMachineStateStopped",
},
{
state: vz.VirtualMachineStateRunning,
want: "VirtualMachineStateRunning",
},
{
state: vz.VirtualMachineStatePaused,
want: "VirtualMachineStatePaused",
},
{
state: vz.VirtualMachineStateError,
want: "VirtualMachineStateError",
},
{
state: vz.VirtualMachineStateStarting,
want: "VirtualMachineStateStarting",
},
{
state: vz.VirtualMachineStatePausing,
want: "VirtualMachineStatePausing",
},
{
state: vz.VirtualMachineStateResuming,
want: "VirtualMachineStateResuming",
},
{
state: vz.VirtualMachineStateStopping,
want: "VirtualMachineStateStopping",
},
}
for _, tc := range cases {
got := tc.state.String()
if tc.want != got {
t.Fatalf("want %q but got %q", tc.want, got)
}
}
}
30 changes: 30 additions & 0 deletions virtualmachinestate_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.