Skip to content

Integrate process handling helpers #25

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

Merged
merged 6 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "1.16"
go-version: "1.21"

- name: Run unit tests
run: go test -v ./... -coverprofile=coverage_unit.txt
Expand Down
80 changes: 80 additions & 0 deletions executils/pipes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// This file is part of PathsHelper library.
//
// Copyright 2023 Arduino AG (http://www.arduino.cc/)
//
// PathsHelper library is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
// As a special exception, you may use this file as part of a free software
// library without restriction. Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License. This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.
//

package executils

import (
"bytes"
"io"
"os/exec"
)

// PipeCommands executes the commands received as input by feeding the output of
// one to the input of the other, exactly like Unix Pipe (|).
// Returns the output of the final command and the eventual error.
//
// code inspired by https://gist.github.com/tyndyll/89fbb2c2273f83a074dc
func PipeCommands(commands ...*exec.Cmd) ([]byte, error) {
var errorBuffer, outputBuffer bytes.Buffer
pipeStack := make([]*io.PipeWriter, len(commands)-1)
i := 0
for ; i < len(commands)-1; i++ {
stdinPipe, stdoutPipe := io.Pipe()
commands[i].Stdout = stdoutPipe
commands[i].Stderr = &errorBuffer
commands[i+1].Stdin = stdinPipe
pipeStack[i] = stdoutPipe
}
commands[i].Stdout = &outputBuffer
commands[i].Stderr = &errorBuffer

if err := call(commands, pipeStack); err != nil {
return nil, err
}

return outputBuffer.Bytes(), nil
}

func call(stack []*exec.Cmd, pipes []*io.PipeWriter) (err error) {
if stack[0].Process == nil {
if err = stack[0].Start(); err != nil {
return err
}
}
if len(stack) > 1 {
if err = stack[1].Start(); err != nil {
return err
}
defer func() {
pipes[0].Close()
err = call(stack[1:], pipes[1:])
}()
}
return stack[0].Wait()
}
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
module github.com/arduino/go-paths-helper

go 1.12
go 1.21

require (
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.3.0
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
13 changes: 8 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
4 changes: 2 additions & 2 deletions gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import (
)

func TestGzipGunzip(t *testing.T) {
zipped := New("_testdata", "test.txt.gz")
unzipped := New("_testdata", "test.txt")
zipped := New("testdata", "fileset", "test.txt.gz")
unzipped := New("testdata", "fileset", "test.txt")

tmp, err := MkTempDir("", "")
require.NoError(t, err)
Expand Down
94 changes: 47 additions & 47 deletions paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func TestPathNew(t *testing.T) {
}

func TestPath(t *testing.T) {
testPath := New("_testdata")
pathEqualsTo(t, "_testdata", testPath)
testPath := New("testdata", "fileset")
pathEqualsTo(t, "testdata/fileset", testPath)
isDir, err := testPath.IsDirCheck()
require.True(t, isDir)
require.NoError(t, err)
Expand All @@ -71,7 +71,7 @@ func TestPath(t *testing.T) {
require.False(t, testPath.NotExist())

folderPath := testPath.Join("folder")
pathEqualsTo(t, "_testdata/folder", folderPath)
pathEqualsTo(t, "testdata/fileset/folder", folderPath)
isDir, err = folderPath.IsDirCheck()
require.True(t, isDir)
require.NoError(t, err)
Expand All @@ -85,7 +85,7 @@ func TestPath(t *testing.T) {
require.False(t, folderPath.NotExist())

filePath := testPath.Join("file")
pathEqualsTo(t, "_testdata/file", filePath)
pathEqualsTo(t, "testdata/fileset/file", filePath)
isDir, err = filePath.IsDirCheck()
require.False(t, isDir)
require.NoError(t, err)
Expand All @@ -98,7 +98,7 @@ func TestPath(t *testing.T) {
require.False(t, filePath.NotExist())

anotherFilePath := filePath.Join("notexistent")
pathEqualsTo(t, "_testdata/file/notexistent", anotherFilePath)
pathEqualsTo(t, "testdata/fileset/file/notexistent", anotherFilePath)
isDir, err = anotherFilePath.IsDirCheck()
require.False(t, isDir)
require.Error(t, err)
Expand All @@ -113,32 +113,32 @@ func TestPath(t *testing.T) {
list, err := folderPath.ReadDir()
require.NoError(t, err)
require.Len(t, list, 4)
pathEqualsTo(t, "_testdata/folder/.hidden", list[0])
pathEqualsTo(t, "_testdata/folder/file2", list[1])
pathEqualsTo(t, "_testdata/folder/file3", list[2])
pathEqualsTo(t, "_testdata/folder/subfolder", list[3])
pathEqualsTo(t, "testdata/fileset/folder/.hidden", list[0])
pathEqualsTo(t, "testdata/fileset/folder/file2", list[1])
pathEqualsTo(t, "testdata/fileset/folder/file3", list[2])
pathEqualsTo(t, "testdata/fileset/folder/subfolder", list[3])

list2 := list.Clone()
list2.FilterDirs()
require.Len(t, list2, 1)
pathEqualsTo(t, "_testdata/folder/subfolder", list2[0])
pathEqualsTo(t, "testdata/fileset/folder/subfolder", list2[0])

list2 = list.Clone()
list2.FilterOutHiddenFiles()
require.Len(t, list2, 3)
pathEqualsTo(t, "_testdata/folder/file2", list2[0])
pathEqualsTo(t, "_testdata/folder/file3", list2[1])
pathEqualsTo(t, "_testdata/folder/subfolder", list2[2])
pathEqualsTo(t, "testdata/fileset/folder/file2", list2[0])
pathEqualsTo(t, "testdata/fileset/folder/file3", list2[1])
pathEqualsTo(t, "testdata/fileset/folder/subfolder", list2[2])

list2 = list.Clone()
list2.FilterOutPrefix("file")
require.Len(t, list2, 2)
pathEqualsTo(t, "_testdata/folder/.hidden", list2[0])
pathEqualsTo(t, "_testdata/folder/subfolder", list2[1])
pathEqualsTo(t, "testdata/fileset/folder/.hidden", list2[0])
pathEqualsTo(t, "testdata/fileset/folder/subfolder", list2[1])
}

func TestResetStatCacheWhenFollowingSymlink(t *testing.T) {
testdata := New("_testdata")
testdata := New("testdata", "fileset")
files, err := testdata.ReadDir()
require.NoError(t, err)
for _, file := range files {
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestIsInsideDir(t *testing.T) {
}

func TestReadFileAsLines(t *testing.T) {
lines, err := New("_testdata/anotherFile").ReadFileAsLines()
lines, err := New("testdata/fileset/anotherFile").ReadFileAsLines()
require.NoError(t, err)
require.Len(t, lines, 4)
require.Equal(t, "line 1", lines[0])
Expand All @@ -226,7 +226,7 @@ func TestCopyDir(t *testing.T) {
require.NoError(t, err)
defer tmp.RemoveAll()

src := New("_testdata")
src := New("testdata", "fileset")
err = src.CopyDirTo(tmp.Join("dest"))
require.NoError(t, err, "copying dir")

Expand Down Expand Up @@ -264,45 +264,45 @@ func TestParents(t *testing.T) {
}

func TestFilterDirs(t *testing.T) {
testPath := New("_testdata")
testPath := New("testdata", "fileset")

list, err := testPath.ReadDir()
require.NoError(t, err)
require.Len(t, list, 6)

pathEqualsTo(t, "_testdata/anotherFile", list[0])
pathEqualsTo(t, "_testdata/file", list[1])
pathEqualsTo(t, "_testdata/folder", list[2])
pathEqualsTo(t, "_testdata/symlinktofolder", list[3])
pathEqualsTo(t, "_testdata/test.txt", list[4])
pathEqualsTo(t, "_testdata/test.txt.gz", list[5])
pathEqualsTo(t, "testdata/fileset/anotherFile", list[0])
pathEqualsTo(t, "testdata/fileset/file", list[1])
pathEqualsTo(t, "testdata/fileset/folder", list[2])
pathEqualsTo(t, "testdata/fileset/symlinktofolder", list[3])
pathEqualsTo(t, "testdata/fileset/test.txt", list[4])
pathEqualsTo(t, "testdata/fileset/test.txt.gz", list[5])

list.FilterDirs()
require.Len(t, list, 2)
pathEqualsTo(t, "_testdata/folder", list[0])
pathEqualsTo(t, "_testdata/symlinktofolder", list[1])
pathEqualsTo(t, "testdata/fileset/folder", list[0])
pathEqualsTo(t, "testdata/fileset/symlinktofolder", list[1])
}

func TestFilterOutDirs(t *testing.T) {
testPath := New("_testdata")
testPath := New("testdata", "fileset")

list, err := testPath.ReadDir()
require.NoError(t, err)
require.Len(t, list, 6)

pathEqualsTo(t, "_testdata/anotherFile", list[0])
pathEqualsTo(t, "_testdata/file", list[1])
pathEqualsTo(t, "_testdata/folder", list[2])
pathEqualsTo(t, "_testdata/symlinktofolder", list[3])
pathEqualsTo(t, "_testdata/test.txt", list[4])
pathEqualsTo(t, "_testdata/test.txt.gz", list[5])
pathEqualsTo(t, "testdata/fileset/anotherFile", list[0])
pathEqualsTo(t, "testdata/fileset/file", list[1])
pathEqualsTo(t, "testdata/fileset/folder", list[2])
pathEqualsTo(t, "testdata/fileset/symlinktofolder", list[3])
pathEqualsTo(t, "testdata/fileset/test.txt", list[4])
pathEqualsTo(t, "testdata/fileset/test.txt.gz", list[5])

list.FilterOutDirs()
require.Len(t, list, 4)
pathEqualsTo(t, "_testdata/anotherFile", list[0])
pathEqualsTo(t, "_testdata/file", list[1])
pathEqualsTo(t, "_testdata/test.txt", list[2])
pathEqualsTo(t, "_testdata/test.txt.gz", list[3])
pathEqualsTo(t, "testdata/fileset/anotherFile", list[0])
pathEqualsTo(t, "testdata/fileset/file", list[1])
pathEqualsTo(t, "testdata/fileset/test.txt", list[2])
pathEqualsTo(t, "testdata/fileset/test.txt.gz", list[3])
}

func TestEquivalentPaths(t *testing.T) {
Expand All @@ -314,8 +314,8 @@ func TestEquivalentPaths(t *testing.T) {
require.True(t, wd.Join("file1").EquivalentTo(New("file1", "abc", "..")))

if runtime.GOOS == "windows" {
q := New("_testdata", "anotherFile")
r := New("_testdata", "ANOTHE~1")
q := New("testdata", "fileset", "anotherFile")
r := New("testdata", "fileset", "ANOTHE~1")
require.True(t, q.EquivalentTo(r))
require.True(t, r.EquivalentTo(q))
}
Expand All @@ -325,15 +325,15 @@ func TestCanonicalize(t *testing.T) {
wd, err := Getwd()
require.NoError(t, err)

p := New("_testdata", "anotherFile").Canonical()
require.Equal(t, wd.Join("_testdata", "anotherFile").String(), p.String())
p := New("testdata", "fileset", "anotherFile").Canonical()
require.Equal(t, wd.Join("testdata", "fileset", "anotherFile").String(), p.String())

p = New("_testdata", "nonexistentFile").Canonical()
require.Equal(t, wd.Join("_testdata", "nonexistentFile").String(), p.String())
p = New("testdata", "fileset", "nonexistentFile").Canonical()
require.Equal(t, wd.Join("testdata", "fileset", "nonexistentFile").String(), p.String())

if runtime.GOOS == "windows" {
q := New("_testdata", "ANOTHE~1").Canonical()
require.Equal(t, wd.Join("_testdata", "anotherFile").String(), q.String())
q := New("testdata", "fileset", "ANOTHE~1").Canonical()
require.Equal(t, wd.Join("testdata", "fileset", "anotherFile").String(), q.String())

r := New("c:\\").Canonical()
require.Equal(t, "C:\\", r.String())
Expand Down Expand Up @@ -371,7 +371,7 @@ func TestRelativeTo(t *testing.T) {
}

func TestWriteToTempFile(t *testing.T) {
tmpDir := New("_testdata", "tmp")
tmpDir := New("testdata", "fileset", "tmp")
err := tmpDir.MkdirAll()
require.NoError(t, err)
defer tmpDir.RemoveAll()
Expand Down
Loading