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

added mechanisms for codesign when testing #72

Merged
merged 3 commits into from
Oct 13, 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
2 changes: 1 addition & 1 deletion .github/workflows/compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
with:
go-version: ${{ matrix.go }}
- name: Unit Test
run: go test ./...
run: make test
- name: Build
run: cd example/linux && make
- name: vet
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.PHONY: fmt
fmt:
@ls | grep -E '\.(h|m)$$' | xargs clang-format -i
@ls | grep -E '\.(h|m)$$' | xargs clang-format -i

.PHONY: test
test:
go test -exec "go run $(PWD)/cmd/codesign" -count=1 ./...
62 changes: 62 additions & 0 deletions cmd/codesign/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"fmt"
"log"
"os"
"os/exec"
"strings"
)

var entitlements = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.virtualization</key>
<true/>
</dict>
</plist>`

func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}

func run() error {
f, err := os.CreateTemp("", "*.entitlements")
if err != nil {
return fmt.Errorf("failed to create temp file: %w", err)
}
defer f.Close()
defer os.Remove(f.Name()) // clean up

if _, err := f.WriteString(entitlements); err != nil {
return fmt.Errorf("failed to write entitlements content: %w", err)
}

if err := f.Close(); err != nil {
return fmt.Errorf("failed to close temp file: %w", err)
}

cmd := exec.Command("codesign", "--entitlements", f.Name(), "-s", "-", os.Args[1])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use a variable codesignArgs := []string { "codesign", "--entitlements", f.Name(), "-s", "-", os.Args[1]} to avoid code duplication when there is an error?

Copy link
Owner Author

@Code-Hex Code-Hex Oct 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is not a large benefit. It's not a part that is frequently modified.

cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to do codesign(%q): %w",
strings.Join(
[]string{
"codesign", "--entitlements", f.Name(), "-s", "-", os.Args[1],
},
" ",
),
err,
)
}

testcmd := exec.Command(os.Args[1], os.Args[2:]...)
testcmd.Stdout = os.Stdout
testcmd.Stderr = os.Stderr
testcmd.Stdin = os.Stdin

return testcmd.Run()
}