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

fix: problems with latest Go1.23 #1

Merged
merged 2 commits into from
Nov 12, 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
31 changes: 31 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Go tests

on:
push:
branches:
- main
paths-ignore:
- "**/*.md"
- "LICENSE"
pull_request:
paths-ignore:
- "**/*.md"
- "LICENSE"

jobs:
test:
strategy:
matrix:
go-version: [1.22.x, 1.23.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
cache: true
- name: Run tests
run: go test ./...
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module github.com/jcchavezs/mergefs

go 1.16
go 1.22

require github.com/stretchr/testify v1.8.2

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
)
11 changes: 7 additions & 4 deletions readfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ func (rfs *readFileFS) ReadFile(name string) (content []byte, err error) {
if err == nil {
return content, nil
} else if !errors.Is(err, os.ErrNotExist) {
if errors.Unwrap(err).Error() != "invalid name" {
// invalid name is returned when the filename
// is an absolute path, which is only supported by
// the OS filesystem
// invalid name is returned when the filename
// is an absolute path, which is only supported by
// the OS filesystem.
// Starting from Go 1.23, os.ErrInvalid is used as error message.
// See go-review.googlesource.com/c/go/+/560137/4/src/io/fs/sub.go
// Both errors are checked to support both versions.
if errors.Unwrap(err).Error() != "invalid name" && !errors.Is(err, os.ErrInvalid) {
return nil, err
}
}
Expand Down
12 changes: 11 additions & 1 deletion readfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"embed"
"io/fs"
"os"
"strings"
"testing"
"testing/fstest"

Expand Down Expand Up @@ -55,7 +56,11 @@ func TestReadfile(t *testing.T) {
var testdataFS embed.FS

func TestAbsolutePath(t *testing.T) {
mfs := Merge(testdataFS, io.OSFS)
// Emulates github.com/corazawaf/coraza-coreruleset/blob/main/coreruleset.go usage of mergefs
rulesFS, err := fs.Sub(testdataFS, "testdata")
require.NoError(t, err)

mfs := Merge(rulesFS, io.OSFS)

f, err := os.CreateTemp(t.TempDir(), "fizz.conf")
require.NoError(t, err)
Expand All @@ -71,4 +76,9 @@ func TestAbsolutePath(t *testing.T) {

_, err = rfmfs.ReadFile(f.Name())
require.NoError(t, err)

_, err = rfmfs.ReadFile("/tmp/doesnotexist.conf")
if !strings.Contains(err.Error(), "no such file or directory") {
t.Errorf("expected not found error. Got: %s", err.Error())
}
}