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

Ci/commitlint #48

Merged
merged 1 commit into from
Jan 31, 2024
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
17 changes: 10 additions & 7 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
name: commit_lint
on: [pull_request]
name: lint

permissions:
contents: read
pull-requests: read
on:
pull_request:
push:
branches:
- "main"

jobs:
commitlint:
staticcheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: wagoid/commitlint-github-action@v5
- uses: actions/setup-go@v3
- run: go install honnef.co/go/tools/cmd/staticcheck@latest
- run: ~/go/bin/staticcheck -checks all
File renamed without changes.
14 changes: 14 additions & 0 deletions .github/workflows/validation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: validation

on: [pull_request]

permissions:
contents: read
pull-requests: read

jobs:
commitlint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: wagoid/commitlint-github-action@v5
7 changes: 5 additions & 2 deletions command.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Package cmd is a simple package
// to execute shell commeand on linux,
// windows, and osx.
package cmd

import (
Expand Down Expand Up @@ -245,12 +248,12 @@ func (c *Command) ExecuteContext(ctx context.Context) error {
select {
case <-ctx.Done():
if err := cmd.Process.Kill(); err != nil {
return fmt.Errorf("Timeout occurred and can not kill process with pid %v", cmd.Process.Pid)
return fmt.Errorf("timeout occurred and can not kill process with pid %v", cmd.Process.Pid)
}

err := ctx.Err()
if c.Timeout > 0 && !hasDeadline {
err = fmt.Errorf("Command timed out after %v", c.Timeout)
err = fmt.Errorf("command timed out after %v", c.Timeout)
}
return err
case err := <-done:
Expand Down
3 changes: 1 addition & 2 deletions command_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

func TestCommand_ExecuteStderr(t *testing.T) {
cmd := NewCommand(">&2 echo hello")

err := cmd.Execute()

assert.Nil(t, err)
Expand All @@ -23,7 +22,7 @@ func TestCommand_WithTimeout(t *testing.T) {
err := cmd.Execute()

assert.NotNil(t, err)
assert.Equal(t, "Command timed out after 5ms", err.Error())
assert.Equal(t, "command timed out after 5ms", err.Error())
}

func TestCommand_WithValidTimeout(t *testing.T) {
Expand Down
14 changes: 7 additions & 7 deletions command_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"bytes"
"context"
"io/ioutil"
"os"
"os/exec"
"strings"
Expand All @@ -12,6 +11,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCommand_ExecuteStderr(t *testing.T) {
Expand All @@ -27,10 +27,9 @@ func TestCommand_WithTimeout(t *testing.T) {
cmd := NewCommand("sleep 0.1;", WithTimeout(1*time.Millisecond))

err := cmd.Execute()

assert.NotNil(t, err)
// Sadly a process can not be killed every time :(
containsMsg := strings.Contains(err.Error(), "Timeout occurred and can not kill process with pid") || strings.Contains(err.Error(), "Command timed out after 1ms")
containsMsg := strings.Contains(err.Error(), "timeout occurred and can not kill process with pid") || strings.Contains(err.Error(), "command timed out after 1ms")
assert.True(t, containsMsg)
}

Expand All @@ -54,7 +53,8 @@ func TestCommand_WithWorkingDir(t *testing.T) {
}

func TestCommand_WithStandardStreams(t *testing.T) {
tmpFile, _ := ioutil.TempFile("/tmp", "stdout_")
tmpFile, err := os.CreateTemp("/tmp", "stdout_")
require.NoError(t, err)
originalStdout := os.Stdout
os.Stdout = tmpFile

Expand All @@ -66,8 +66,8 @@ func TestCommand_WithStandardStreams(t *testing.T) {
cmd := NewCommand("echo hey", WithStandardStreams)
cmd.Execute()

r, err := ioutil.ReadFile(tmpFile.Name())
assert.Nil(t, err)
r, err := os.ReadFile(tmpFile.Name())
require.NoError(t, err)
assert.Equal(t, "hey\n", string(r))
}

Expand Down Expand Up @@ -137,7 +137,7 @@ func TestCommand_WithContext(t *testing.T) {
cmd := NewCommand("sleep 3;", WithTimeout(1*time.Second))
err := cmd.Execute()
assert.NotNil(t, err)
assert.Equal(t, "Command timed out after 1s", err.Error())
assert.Equal(t, "command timed out after 1s", err.Error())

// set context timeout to 2 seconds to ensure
// context takes precedence over timeout
Expand Down
3 changes: 1 addition & 2 deletions command_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ func TestCommand_ExecuteStderr(t *testing.T) {

func TestCommand_WithTimeout(t *testing.T) {
cmd := NewCommand("timeout 0.005;", WithTimeout(5*time.Millisecond))

err := cmd.Execute()

assert.NotNil(t, err)
// This is needed because windows sometimes can not kill the process :(
containsMsg := strings.Contains(err.Error(), "Timeout occurred and can not kill process with pid") || strings.Contains(err.Error(), "Command timed out after 5ms")
containsMsg := strings.Contains(err.Error(), "timeout occurred and can not kill process with pid") || strings.Contains(err.Error(), "command timed out after 5ms")
assert.True(t, containsMsg)
}

Expand Down
29 changes: 29 additions & 0 deletions vendor/github.com/stretchr/testify/require/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions vendor/github.com/stretchr/testify/require/forward_requirements.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading