Skip to content

Commit d30f506

Browse files
committed
support once for maxAllowedVersion
1 parent 7846ca9 commit d30f506

7 files changed

+45
-34
lines changed

network_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestFileHandleNetworkDeviceAttachmentMTU(t *testing.T) {
11-
if vz.MacosMajorVersionLessThan(13) {
11+
if vz.Available(13) {
1212
t.Skip("FileHandleNetworkDeviceAttachment.SetMaximumTransmissionUnit is supported from macOS 13")
1313
}
1414

osversion.go

+20-12
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,9 @@ func macOSAvailable(version float64) error {
3434
return macOSBuildTargetAvailable(version)
3535
}
3636

37-
func macosMajorVersionLessThan(major float64) bool {
38-
return macOSMajorMinorVersion() < major
39-
}
40-
4137
var (
42-
majorVersion float64
43-
majorVersionOnce interface{ Do(func()) } = &sync.Once{}
38+
majorMinorVersion float64
39+
majorMinorVersionOnce interface{ Do(func()) } = &sync.Once{}
4440

4541
// This can be replaced in the test code to enable mock.
4642
// It will not be changed in production.
@@ -62,23 +58,35 @@ func fetchMajorMinorVersion() (float64, error) {
6258
}
6359

6460
func macOSMajorMinorVersion() float64 {
65-
majorVersionOnce.Do(func() {
61+
majorMinorVersionOnce.Do(func() {
6662
version, err := fetchMajorMinorVersion()
6763
if err != nil {
6864
panic(err)
6965
}
70-
majorVersion = version
66+
majorMinorVersion = version
7167
})
72-
return majorVersion
68+
return majorMinorVersion
7369
}
7470

75-
var maxAllowedVersion = func() int {
76-
return int(C.mac_os_x_version_max_allowed())
71+
var (
72+
maxAllowedVersion int
73+
maxAllowedVersionOnce interface{ Do(func()) } = &sync.Once{}
74+
75+
getMaxAllowedVersion = func() int {
76+
return int(C.mac_os_x_version_max_allowed())
77+
}
78+
)
79+
80+
func fetchMaxAllowedVersion() int {
81+
maxAllowedVersionOnce.Do(func() {
82+
maxAllowedVersion = getMaxAllowedVersion()
83+
})
84+
return maxAllowedVersion
7785
}
7886

7987
// macOSBuildTargetAvailable checks whether the API available in a given version has been compiled.
8088
func macOSBuildTargetAvailable(version float64) error {
81-
allowedVersion := maxAllowedVersion()
89+
allowedVersion := fetchMaxAllowedVersion()
8290
if allowedVersion == 0 {
8391
return fmt.Errorf("undefined __MAC_OS_X_VERSION_MAX_ALLOWED: %w", ErrBuildTargetOSVersion)
8492
}

osversion_arm64_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import (
1111
)
1212

1313
func TestAvailableVersionArm64(t *testing.T) {
14-
majorVersionOnce = &nopDoer{}
14+
majorMinorVersionOnce = &nopDoer{}
1515
defer func() {
16-
majorVersion = 0
17-
majorVersionOnce = &sync.Once{}
16+
majorMinorVersion = 0
17+
majorMinorVersionOnce = &sync.Once{}
1818
}()
1919
t.Run("macOS 12", func(t *testing.T) {
20-
majorVersion = 11
20+
majorMinorVersion = 11
2121
cases := map[string]func() error{
2222
"NewMacOSBootLoader": func() error {
2323
_, err := NewMacOSBootLoader()

osversion_test.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ type nopDoer struct{}
1414
func (*nopDoer) Do(func()) {}
1515

1616
func TestAvailableVersion(t *testing.T) {
17-
majorVersionOnce = &nopDoer{}
17+
majorMinorVersionOnce = &nopDoer{}
1818
defer func() {
19-
majorVersion = 0
20-
majorVersionOnce = &sync.Once{}
19+
majorMinorVersion = 0
20+
majorMinorVersionOnce = &sync.Once{}
2121
}()
2222

2323
t.Run("macOS 11", func(t *testing.T) {
24-
majorVersion = 10
24+
majorMinorVersion = 10
2525
cases := map[string]func() error{
2626
"NewLinuxBootLoader": func() error {
2727
_, err := NewLinuxBootLoader("")
@@ -105,7 +105,7 @@ func TestAvailableVersion(t *testing.T) {
105105
})
106106

107107
t.Run("macOS 12", func(t *testing.T) {
108-
majorVersion = 11
108+
majorMinorVersion = 11
109109
cases := map[string]func() error{
110110
"NewVirtioSoundDeviceConfiguration": func() error {
111111
_, err := NewVirtioSoundDeviceConfiguration()
@@ -222,6 +222,11 @@ func Test_fetchMajorMinorVersion(t *testing.T) {
222222
}
223223

224224
func Test_macOSBuildTargetAvailable(t *testing.T) {
225+
maxAllowedVersionOnce = &nopDoer{}
226+
defer func() {
227+
maxAllowedVersionOnce = &sync.Once{}
228+
}()
229+
225230
cases := []struct {
226231
// version is specified only 11, 12, 12.3, 13
227232
version float64
@@ -308,11 +313,9 @@ func Test_macOSBuildTargetAvailable(t *testing.T) {
308313
tc.version,
309314
)
310315
t.Run(name, func(t *testing.T) {
311-
_maxAllowedVersion := maxAllowedVersion
312-
defer func() { maxAllowedVersion = _maxAllowedVersion }()
313-
maxAllowedVersion = func() int {
314-
return tc.maxAllowedVersion
315-
}
316+
tmp := maxAllowedVersion
317+
defer func() { maxAllowedVersion = tmp }()
318+
maxAllowedVersion = tc.maxAllowedVersion
316319

317320
err := macOSBuildTargetAvailable(tc.version)
318321
if (err != nil) != tc.wantErr {

shared_directory_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
func TestVirtioFileSystemDeviceConfigurationTag(t *testing.T) {
16-
if vz.MacosMajorVersionLessThan(12) {
16+
if vz.Available(12) {
1717
t.Skip("VirtioFileSystemDeviceConfiguration is supported from macOS 12")
1818
}
1919

@@ -32,7 +32,7 @@ func TestVirtioFileSystemDeviceConfigurationTag(t *testing.T) {
3232
}
3333

3434
func TestSingleDirectoryShare(t *testing.T) {
35-
if vz.MacosMajorVersionLessThan(12) {
35+
if vz.Available(12) {
3636
t.Skip("SingleDirectoryShare is supported from macOS 12")
3737
}
3838

@@ -146,7 +146,7 @@ func TestSingleDirectoryShare(t *testing.T) {
146146
}
147147

148148
func TestMultipleDirectoryShare(t *testing.T) {
149-
if vz.MacosMajorVersionLessThan(12) {
149+
if vz.Available(12) {
150150
t.Skip("MultipleDirectoryShare is supported from macOS 12")
151151
}
152152

virtualization_export_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package vz
22

3-
func MacosMajorVersionLessThan(version float64) bool {
4-
return macosMajorVersionLessThan(version)
3+
func Available(version float64) bool {
4+
return macOSAvailable(version) != nil
55
}

virtualization_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func newVirtualizationMachine(
172172
//
173173
// This is a workaround. This version of the API does not immediately return an error and
174174
// does not seem to have a connection timeout set.
175-
if vz.MacosMajorVersionLessThan(12) {
175+
if vz.Available(12) {
176176
time.Sleep(5 * time.Second)
177177
}
178178

@@ -301,7 +301,7 @@ func TestRun(t *testing.T) {
301301
}
302302

303303
func TestStop(t *testing.T) {
304-
if vz.MacosMajorVersionLessThan(12) {
304+
if vz.Available(12) {
305305
t.Skip("Stop is supported from macOS 12")
306306
}
307307

0 commit comments

Comments
 (0)