Skip to content

Commit 4f0c32d

Browse files
committed
cmd/dist: log OS version when testing
As a follow-up to https://golang.org/cl/371474, add the OS version to the metadata printed for each test. Fixes #50146. Change-Id: I3b7e47983d0e85feebce8e424881b931882d53bf Reviewed-on: https://go-review.googlesource.com/c/go/+/371475 Reviewed-by: Bryan Mills <[email protected]> Trust: Michael Pratt <[email protected]> Run-TryBot: Michael Pratt <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Austin Clements <[email protected]>
1 parent 1302f93 commit 4f0c32d

File tree

6 files changed

+109
-0
lines changed

6 files changed

+109
-0
lines changed

src/cmd/dist/metadata.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
package main
1313

1414
import (
15+
"cmd/internal/osinfo"
1516
"fmt"
1617
"internal/sysinfo"
1718
"runtime"
@@ -20,5 +21,13 @@ import (
2021
func logMetadata() error {
2122
fmt.Printf("# GOARCH: %s\n", runtime.GOARCH)
2223
fmt.Printf("# CPU: %s\n", sysinfo.CPU.Name())
24+
25+
fmt.Printf("# GOOS: %s\n", runtime.GOOS)
26+
ver, err := osinfo.Version()
27+
if err != nil {
28+
return fmt.Errorf("error determining OS version: %v", err)
29+
}
30+
fmt.Printf("# OS Version: %s\n", ver)
31+
2332
return nil
2433
}

src/cmd/internal/osinfo/doc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package osinfo provides OS metadata information.
6+
package osinfo

src/cmd/internal/osinfo/os_js.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build js
6+
7+
package osinfo
8+
9+
import (
10+
"fmt"
11+
)
12+
13+
// Version returns the OS version name/number.
14+
func Version() (string, error) {
15+
// TODO(prattmic): Does wasm have any version/runtime detection
16+
// functionality?
17+
return "", fmt.Errorf("unimplemented")
18+
}

src/cmd/internal/osinfo/os_plan9.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build plan9
6+
7+
package osinfo
8+
9+
import (
10+
"os"
11+
)
12+
13+
// Version returns the OS version name/number.
14+
func Version() (string, error) {
15+
b, err := os.ReadFile("/dev/osversion")
16+
if err != nil {
17+
return "", err
18+
}
19+
20+
return string(b), nil
21+
}

src/cmd/internal/osinfo/os_unix.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
6+
7+
package osinfo
8+
9+
import (
10+
"bytes"
11+
12+
"golang.org/x/sys/unix"
13+
)
14+
15+
func utsString(b []byte) string {
16+
i := bytes.IndexByte(b, 0)
17+
if i == -1 {
18+
return string(b)
19+
}
20+
return string(b[:i])
21+
}
22+
23+
// Version returns the OS version name/number.
24+
func Version() (string, error) {
25+
var uts unix.Utsname
26+
if err := unix.Uname(&uts); err != nil {
27+
return "", err
28+
}
29+
30+
sysname := utsString(uts.Sysname[:])
31+
release := utsString(uts.Release[:])
32+
version := utsString(uts.Version[:])
33+
machine := utsString(uts.Machine[:])
34+
35+
return sysname + " " + release + " " + version + " " + machine, nil
36+
}

src/cmd/internal/osinfo/os_windows.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build windows
6+
7+
package osinfo
8+
9+
import (
10+
"fmt"
11+
12+
"golang.org/x/sys/windows"
13+
)
14+
15+
// Version returns the OS version name/number.
16+
func Version() (string, error) {
17+
major, minor, patch := windows.RtlGetNtVersionNumbers()
18+
return fmt.Sprintf("%d.%d.%d", major, minor, patch), nil
19+
}

0 commit comments

Comments
 (0)