diff --git a/Makefile b/Makefile index a77a4b7..4bd73a3 100644 --- a/Makefile +++ b/Makefile @@ -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: @@ -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} \ No newline at end of file diff --git a/cmd/addtags/main.go b/cmd/addtags/main.go new file mode 100644 index 0000000..e15b2fc --- /dev/null +++ b/cmd/addtags/main.go @@ -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 -file FILE +`[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) + } +} diff --git a/linuxrosettaavailability_string_arm64.go b/linuxrosettaavailability_string_arm64.go new file mode 100644 index 0000000..f64cadb --- /dev/null +++ b/linuxrosettaavailability_string_arm64.go @@ -0,0 +1,28 @@ +//go:build darwin && arm64 +// +build darwin,arm64 + +// Code generated by "stringer -type=LinuxRosettaAvailability -output=linuxrosettaavailability_string_arm64.go"; DO NOT EDIT. + +package vz + +import "strconv" + +func _() { + // An "invalid array index" compiler error signifies that the constant values have changed. + // Re-run the stringer command to generate them again. + var x [1]struct{} + _ = x[LinuxRosettaAvailabilityNotSupported-0] + _ = x[LinuxRosettaAvailabilityNotInstalled-1] + _ = x[LinuxRosettaAvailabilityInstalled-2] +} + +const _LinuxRosettaAvailability_name = "LinuxRosettaAvailabilityNotSupportedLinuxRosettaAvailabilityNotInstalledLinuxRosettaAvailabilityInstalled" + +var _LinuxRosettaAvailability_index = [...]uint8{0, 36, 72, 105} + +func (i LinuxRosettaAvailability) String() string { + if i < 0 || i >= LinuxRosettaAvailability(len(_LinuxRosettaAvailability_index)-1) { + return "LinuxRosettaAvailability(" + strconv.FormatInt(int64(i), 10) + ")" + } + return _LinuxRosettaAvailability_name[_LinuxRosettaAvailability_index[i]:_LinuxRosettaAvailability_index[i+1]] +} diff --git a/shared_directory_arm64.go b/shared_directory_arm64.go index 36c3c4e..5edab3a 100644 --- a/shared_directory_arm64.go +++ b/shared_directory_arm64.go @@ -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 ( diff --git a/shared_directory_arm64_test.go b/shared_directory_arm64_test.go new file mode 100644 index 0000000..71aefec --- /dev/null +++ b/shared_directory_arm64_test.go @@ -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) + } + } +} diff --git a/virtualization.go b/virtualization.go index 83a0953..b8b5437 100644 --- a/virtualization.go +++ b/virtualization.go @@ -23,6 +23,8 @@ func init() { } // VirtualMachineState represents execution state of the virtual machine. +// +//go:generate stringer -type=VirtualMachineState type VirtualMachineState int const ( diff --git a/virtualization_test.go b/virtualization_test.go index a3a1ce9..a84408c 100644 --- a/virtualization_test.go +++ b/virtualization_test.go @@ -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) + } + } +} diff --git a/virtualmachinestate_string.go b/virtualmachinestate_string.go new file mode 100644 index 0000000..87c16d5 --- /dev/null +++ b/virtualmachinestate_string.go @@ -0,0 +1,30 @@ +// Code generated by "stringer -type=VirtualMachineState"; DO NOT EDIT. + +package vz + +import "strconv" + +func _() { + // An "invalid array index" compiler error signifies that the constant values have changed. + // Re-run the stringer command to generate them again. + var x [1]struct{} + _ = x[VirtualMachineStateStopped-0] + _ = x[VirtualMachineStateRunning-1] + _ = x[VirtualMachineStatePaused-2] + _ = x[VirtualMachineStateError-3] + _ = x[VirtualMachineStateStarting-4] + _ = x[VirtualMachineStatePausing-5] + _ = x[VirtualMachineStateResuming-6] + _ = x[VirtualMachineStateStopping-7] +} + +const _VirtualMachineState_name = "VirtualMachineStateStoppedVirtualMachineStateRunningVirtualMachineStatePausedVirtualMachineStateErrorVirtualMachineStateStartingVirtualMachineStatePausingVirtualMachineStateResumingVirtualMachineStateStopping" + +var _VirtualMachineState_index = [...]uint8{0, 26, 52, 77, 101, 128, 154, 181, 208} + +func (i VirtualMachineState) String() string { + if i < 0 || i >= VirtualMachineState(len(_VirtualMachineState_index)-1) { + return "VirtualMachineState(" + strconv.FormatInt(int64(i), 10) + ")" + } + return _VirtualMachineState_name[_VirtualMachineState_index[i]:_VirtualMachineState_index[i+1]] +}