Skip to content

Commit

Permalink
optimize ci
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Nov 17, 2022
1 parent ec98677 commit 2f1971a
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 27 deletions.
4 changes: 2 additions & 2 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ Object model:

## Run tests

First, set this environment variable:
First, launch a test shell for rod:

```bash
export GODEBUG="tracebackancestors=1000"
go run ./lib/utils/shell
```

Then, no magic, just `go test`. Read the test template [rod_test.go](../rod_test.go) to get started.
Expand Down
5 changes: 1 addition & 4 deletions .github/workflows/test-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ on:
schedule:
- cron: '17 5 * * *'

env:
GODEBUG: tracebackancestors=1000

jobs:
test-linux:
runs-on: ubuntu-latest
Expand All @@ -30,7 +27,7 @@ jobs:

- run: go generate

- run: go test -race -coverprofile=coverage.out ./...
- run: go run ./lib/utils/ci-test -race -coverprofile=coverage.out ./...

- run: go run ./lib/utils/check-cov

Expand Down
7 changes: 2 additions & 5 deletions .github/workflows/test-other-platforms.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ on:

pull_request:

env:
GODEBUG: tracebackancestors=1000

jobs:
test-mac:
runs-on: macos-latest
Expand All @@ -21,7 +18,7 @@ jobs:

- uses: actions/checkout@v2

- run: go test -timeout-each=2m
- run: go run ./lib/utils/ci-test -timeout-each=2m

- uses: actions/upload-artifact@v2
if: ${{ always() }}
Expand All @@ -39,7 +36,7 @@ jobs:

- uses: actions/checkout@v2

- run: go test -timeout-each=2m
- run: go run ./lib/utils/ci-test -timeout-each=2m

- uses: actions/upload-artifact@v2
if: ${{ always() }}
Expand Down
4 changes: 4 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"breakpad",
"Chromedp",
"codesearch",
"COMSPEC",
"containerenv",
"contenteditable",
"Contentful",
Expand All @@ -39,6 +40,7 @@
"fontconfig",
"Fullscreen",
"Geolocation",
"getent",
"gobwas",
"GODEBUG",
"gofmt",
Expand All @@ -65,6 +67,7 @@
"libnss",
"libxss",
"libxtst",
"Lmsgprefix",
"loglevel",
"MDPI",
"MITM",
Expand All @@ -76,6 +79,7 @@
"onmouseenter",
"onmouseout",
"opencontainers",
"osversion",
"progresser",
"proto",
"proxyauth",
Expand Down
2 changes: 0 additions & 2 deletions lib/docker/dev.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,5 @@ RUN curl -L $golang > golang.tar.gz && \

ENV PATH="/usr/local/lib/.node/bin:/usr/local/lib/go/bin:/root/go/bin/:${PATH}"

ENV GODEBUG="tracebackancestors=1000"

# setup global git ignore
RUN git config --global core.excludesfile ~/.gitignore_global
2 changes: 1 addition & 1 deletion lib/docker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func test(at archType) {
if at != archArm {
wd, err := os.Getwd()
utils.E(err)
utils.Exec("docker run -w=/t -v", fmt.Sprintf("%s:/t", wd), at.tagDev(), "go", "test")
utils.Exec("docker run -w=/t -v", fmt.Sprintf("%s:/t", wd), at.tagDev(), "go", "run", "./lib/utils/ci-test")
}
}

Expand Down
16 changes: 16 additions & 0 deletions lib/utils/ci-test/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Package main A helper to run go test on CI with the right environment variables.
package main

import (
"os"

"github.com/go-rod/rod/lib/utils"
)

func main() {
for k, v := range utils.TestEnvs {
err := os.Setenv(k, v)
utils.E(err)
}
utils.Exec("go test", os.Args[1:]...)
}
10 changes: 0 additions & 10 deletions lib/utils/lint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,10 @@
package main

import (
"fmt"
"os"

"github.com/go-rod/rod/lib/utils"
)

func main() {
defer func() {
if err := recover(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}()

utils.Exec("npx -ys -- [email protected] --no-progress **")

utils.Exec("npx -ys -- [email protected] --config=lib/utils/lint/eslint.yml --ext=.js,.html --fix --ignore-path=.gitignore .")
Expand Down
29 changes: 29 additions & 0 deletions lib/utils/shell/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Package main It helps to launcher a transparent shell under the current shell with
// some extra environment variables that are required by rod testing.
package main

import (
"os"
"os/exec"

"github.com/go-rod/rod/lib/utils"
)

func main() {
list := []string{}
for k, v := range utils.TestEnvs {
list = append(list, k+"="+v)
}

bin, err := Shell()
utils.E(err)

cmd := exec.Command(bin)
cmd.Env = append(os.Environ(), list...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

_ = cmd.Run()
os.Exit(cmd.ProcessState.ExitCode())
}
94 changes: 94 additions & 0 deletions lib/utils/shell/shell.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package main

import (
"errors"
"fmt"
"os"
"os/exec"
"os/user"
"regexp"
"runtime"
"strings"
)

// Shell https://github.com/riywo/loginshell/blob/master/loginshell.go
func Shell() (string, error) {
switch runtime.GOOS {
case "plan9":
return plan9Shell()
case "linux":
return nixShell()
case "openbsd":
return nixShell()
case "freebsd":
return nixShell()
case "android":
return androidShell()
case "darwin":
return darwinShell()
case "windows":
return windowsShell()
}

return "", errors.New("Undefined GOOS: " + runtime.GOOS)
}

func plan9Shell() (string, error) {
if _, err := os.Stat("/dev/osversion"); err != nil {
if os.IsNotExist(err) {
return "", err
}
return "", errors.New("/dev/osversion check failed")
}

return "/bin/rc", nil
}

func nixShell() (string, error) {
user, err := user.Current()
if err != nil {
return "", err
}

out, err := exec.Command("getent", "passwd", user.Uid).Output()
if err != nil {
return "", err
}

ent := strings.Split(strings.TrimSuffix(string(out), "\n"), ":")
return ent[6], nil
}

func androidShell() (string, error) {
shell := os.Getenv("SHELL")
if shell == "" {
return "", errors.New("shell not defined in android")
}
return shell, nil
}

func darwinShell() (string, error) {
dir := "Local/Default/Users/" + os.Getenv("USER")
out, err := exec.Command("dscl", "localhost", "-read", dir, "UserShell").Output()
if err != nil {
return "", err
}

re := regexp.MustCompile("UserShell: (/[^ ]+)\n")
matched := re.FindStringSubmatch(string(out))
shell := matched[1]
if shell == "" {
return "", fmt.Errorf("Invalid output: %s", string(out))
}

return shell, nil
}

func windowsShell() (string, error) {
consoleApp := os.Getenv("COMSPEC")
if consoleApp == "" {
consoleApp = "cmd.exe"
}

return consoleApp, nil
}
8 changes: 6 additions & 2 deletions lib/utils/simple-check/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Package main ...
package main

import "github.com/go-rod/rod/lib/utils"
import (
"github.com/go-rod/rod/lib/utils"
)

func main() {
utils.Exec("go run github.com/ysmood/golangci-lint@latest")
utils.Exec("go run ./lib/utils/setup")

utils.Exec("go run ./lib/utils/lint")

utils.Exec("go test -coverprofile=coverage.out ./lib/launcher")
utils.Exec("go run ./lib/utils/check-cov")
Expand Down
7 changes: 6 additions & 1 deletion lib/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import (
"github.com/ysmood/gson"
)

// TestEnvs for testing
var TestEnvs = map[string]string{
"GODEBUG": "tracebackancestors=100",
}

// InContainer will be true if is inside container environment, such as docker
var InContainer = FileExists("/.dockerenv") || FileExists("/.containerenv")

Expand Down Expand Up @@ -274,7 +279,7 @@ func Exec(line string, rest ...string) string {
return ExecLine(true, line, rest...)
}

var execLogger = log.New(os.Stdout, "[exec]", log.LstdFlags)
var execLogger = log.New(os.Stdout, "[exec] ", 0)

// ExecLine of command
func ExecLine(std bool, line string, rest ...string) string {
Expand Down
3 changes: 3 additions & 0 deletions lib/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ func TestExecErr(t *testing.T) {
g.Panic(func() {
utils.Exec(g.RandStr(16))
})
g.Panic(func() {
utils.ExecLine(false, "", "")
})
}

func TestFormatCLIArgs(t *testing.T) {
Expand Down

0 comments on commit 2f1971a

Please sign in to comment.