Files
gf/os/gfile/gfile_z_unit_cache_test.go
hailaz 90564f9fb0 feat(os/gfile): add MatchGlob function with globstar support (#4570) (#4574)
This pull request introduces a new glob pattern matching utility to the
`gfile` package, adding support for advanced glob patterns including the
"**" (globstar) operator, which matches across directory boundaries,
similar to bash and gitignore. It also includes a comprehensive set of
unit tests to verify the correctness and cross-platform compatibility of
the new functionality.

**Glob pattern matching feature:**

* Added `MatchGlob` function to `gfile`, which extends `filepath.Match`
with support for the "**" (globstar) pattern, enabling recursive
directory matching and more flexible file pattern matching.
* Implemented internal helpers (`matchGlobstar` and `doMatchGlobstar`)
to handle normalization of path separators and recursive matching logic
for patterns containing "**".

**Testing and validation:**

* Added `gfile_z_unit_match_test.go` with extensive unit tests covering
basic glob patterns, globstar usage, prefix/suffix combinations,
multiple globstars, edge cases, and Windows path compatibility to ensure
robust and cross-platform behavior.

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: houseme <housemecn@gmail.com>
2025-12-26 16:37:45 +08:00

146 lines
3.0 KiB
Go

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gfile_test
import (
"os"
"testing"
"time"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
)
func Test_GetContentsWithCache(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var f *os.File
var err error
fileName := "test"
strTest := "123"
if !gfile.Exists(fileName) {
f, err = os.CreateTemp("", fileName)
if err != nil {
t.Error("create file fail")
}
}
defer f.Close()
defer os.Remove(f.Name())
if gfile.Exists(f.Name()) {
f, err = gfile.OpenFile(f.Name(), os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
t.Error("file open fail", err)
}
err = gfile.PutContents(f.Name(), strTest)
if err != nil {
t.Error("write error", err)
}
cache := gfile.GetContentsWithCache(f.Name(), 1)
t.Assert(cache, strTest)
}
})
gtest.C(t, func(t *gtest.T) {
var f *os.File
var err error
fileName := "test2"
strTest := "123"
if !gfile.Exists(fileName) {
f, err = os.CreateTemp("", fileName)
if err != nil {
t.Error("create file fail")
}
}
defer f.Close()
defer os.Remove(f.Name())
if gfile.Exists(f.Name()) {
cache := gfile.GetContentsWithCache(f.Name())
f, err = gfile.OpenFile(f.Name(), os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
t.Error("file open fail", err)
}
err = gfile.PutContents(f.Name(), strTest)
if err != nil {
t.Error("write error", err)
}
t.Assert(cache, "")
time.Sleep(100 * time.Millisecond)
}
})
}
func Test_GetBytesWithCache(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var f *os.File
var err error
fileName := "test_bytes"
byteContent := []byte{0x48, 0x65, 0x6c, 0x6c, 0x6f} // "Hello"
if !gfile.Exists(fileName) {
f, err = os.CreateTemp("", fileName)
if err != nil {
t.Error("create file fail")
}
}
defer f.Close()
defer os.Remove(f.Name())
if gfile.Exists(f.Name()) {
err = gfile.PutBytes(f.Name(), byteContent)
if err != nil {
t.Error("write error", err)
}
// Test GetBytesWithCache with custom duration
cache := gfile.GetBytesWithCache(f.Name(), time.Second*1)
t.Assert(cache, byteContent)
// Test cache hit - should return same content
cache2 := gfile.GetBytesWithCache(f.Name(), time.Second*1)
t.Assert(cache2, byteContent)
}
})
// Test with non-existent file
gtest.C(t, func(t *gtest.T) {
cache := gfile.GetBytesWithCache("/nonexistent_file_12345.txt")
t.Assert(cache, nil)
})
// Test with empty file
gtest.C(t, func(t *gtest.T) {
var f *os.File
var err error
fileName := "test_bytes_empty"
f, err = os.CreateTemp("", fileName)
if err != nil {
t.Error("create file fail")
}
defer f.Close()
defer os.Remove(f.Name())
// Read empty file
cache := gfile.GetBytesWithCache(f.Name(), time.Second*1)
t.Assert(len(cache), 0)
})
}