detached cb589c5

add tests for dlfcn (#96)

This commit adds two tests. One comes from an example which wasn't really an example of using purego but more of a test to make sure fakecgo was functioning properly. Now it's an actual test. The other is a simple test that calls into libc
commit
cb589c51828bd47bef6f24344747252ea3e5fdc7
author
TotallyGamerJet · · 2023-02-12 20:02 UTC
committer
GitHub · · 2023-02-12 20:02 UTC
parent
e9c3434
signature
GPG Unverified4AEE18F83AFDEB23 The key is unknown
2 files · +45 -7
A dlfcn_test.go +39 -0
@@ -0,0 +1,39 @@
1+// SPDX-License-Identifier: Apache-2.0
2+// SPDX-FileCopyrightText: 2023 The Ebitengine Authors
3+
4+//go:build darwin || linux
5+
6+package purego_test
7+
8+import (
9+ "fmt"
10+ "runtime"
11+ "testing"
12+
13+ "github.com/ebitengine/purego"
14+)
15+
16+func getSystemLibrary() (string, error) {
17+ switch runtime.GOOS {
18+ case "darwin":
19+ return "/usr/lib/libSystem.B.dylib", nil
20+ case "linux":
21+ return "libc.so.6", nil
22+ default:
23+ return "", fmt.Errorf("GOOS=%s is not supported", runtime.GOOS)
24+ }
25+}
26+
27+func TestRegisterFunc(t *testing.T) {
28+ library, err := getSystemLibrary()
29+ if err != nil {
30+ t.Errorf("couldn't get system library: %s", err)
31+ }
32+ libc := purego.Dlopen(library, purego.RTLD_NOW|purego.RTLD_GLOBAL)
33+ if err := purego.Dlerror(); err != "" {
34+ t.Errorf("failed to dlopen: %s", err)
35+ }
36+ var puts func(string)
37+ purego.RegisterLibFunc(&puts, libc, "puts")
38+ puts("Calling C from from Go without Cgo!")
39+}
@@ -0,0 +1,39 @@
1+// SPDX-License-Identifier: Apache-2.0
2+// SPDX-FileCopyrightText: 2023 The Ebitengine Authors
3+
4+//go:build darwin || linux
5+
6+package purego_test
7+
8+import (
9+ "fmt"
10+ "runtime"
11+ "testing"
12+
13+ "github.com/ebitengine/purego"
14+)
15+
16+func getSystemLibrary() (string, error) {
17+ switch runtime.GOOS {
18+ case "darwin":
19+ return "/usr/lib/libSystem.B.dylib", nil
20+ case "linux":
21+ return "libc.so.6", nil
22+ default:
23+ return "", fmt.Errorf("GOOS=%s is not supported", runtime.GOOS)
24+ }
25+}
26+
27+func TestRegisterFunc(t *testing.T) {
28+ library, err := getSystemLibrary()
29+ if err != nil {
30+ t.Errorf("couldn't get system library: %s", err)
31+ }
32+ libc := purego.Dlopen(library, purego.RTLD_NOW|purego.RTLD_GLOBAL)
33+ if err := purego.Dlerror(); err != "" {
34+ t.Errorf("failed to dlopen: %s", err)
35+ }
36+ var puts func(string)
37+ purego.RegisterLibFunc(&puts, libc, "puts")
38+ puts("Calling C from from Go without Cgo!")
39+}
R example/main.go → syscall_test.go +6 -7 renamed, 58% similar
@@ -1,24 +1,23 @@
11 // SPDX-License-Identifier: Apache-2.0
2-// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
2+// SPDX-FileCopyrightText: 2023 The Ebitengine Authors
33
4-//go:build darwin || linux || windows
5-
6-package main
4+package purego_test
75
86 import (
97 "os"
8+ "testing"
109
1110 _ "github.com/ebitengine/purego"
1211 )
1312
14-func main() {
13+func TestOS(t *testing.T) {
1514 // set and unset an environment variable since this calls into fakecgo.
1615 err := os.Setenv("TESTING", "SOMETHING")
1716 if err != nil {
18- panic(err)
17+ t.Errorf("failed to Setenv: %s", err)
1918 }
2019 err = os.Unsetenv("TESTING")
2120 if err != nil {
22- panic(err)
21+ t.Errorf("failed to Unsetenv: %s", err)
2322 }
2423 }
@@ -1,24 +1,23 @@
1 // SPDX-License-Identifier: Apache-2.01 // SPDX-License-Identifier: Apache-2.0
2-// SPDX-FileCopyrightText: 2022 The Ebitengine Authors2+// SPDX-FileCopyrightText: 2023 The Ebitengine Authors
3 3
4-//go:build darwin || linux || windows4+package purego_test
5-
6-package main
7 5
8 import (6 import (
9 "os"7 "os"
8+ "testing"
10 9
11 _ "github.com/ebitengine/purego"10 _ "github.com/ebitengine/purego"
12 )11 )
13 12
14-func main() {13+func TestOS(t *testing.T) {
15 // set and unset an environment variable since this calls into fakecgo.14 // set and unset an environment variable since this calls into fakecgo.
16 err := os.Setenv("TESTING", "SOMETHING")15 err := os.Setenv("TESTING", "SOMETHING")
17 if err != nil {16 if err != nil {
18- panic(err)17+ t.Errorf("failed to Setenv: %s", err)
19 }18 }
20 err = os.Unsetenv("TESTING")19 err = os.Unsetenv("TESTING")
21 if err != nil {20 if err != nil {
22- panic(err)21+ t.Errorf("failed to Unsetenv: %s", err)
23 }22 }
24 }23 }

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 102

↑↓ moveenter openesc close