Files
Michael BiancoandGitHub 3fee1b57cd refactor: update to go 1.21+ syntax and idioms (#524)
* refactor: update to go 1.21+ syntax and idioms

- Replace interface{} with any throughout the codebase.
- Use strings.SplitSeq and strings.CutPrefix for more efficient string processing.
- Utilize slices.Contains for cleaner slice membership checks.
- Use strings.Builder for efficient string concatenation.
- Adopt modern loop syntax (for range) for iteration.
- Remove redundant omitempty tags in struct definitions.

Generated-by: aiautocommit

* refactor: linting fix

* build: standardize commit messages for automation tasks

- Update commit messages in the upgrade task to follow conventional commit standards.
- Ensure consistency across automated fix, formatting, and snapshot update processes.

Generated-by: aiautocommit

* refactor: update commit messages in mise-upgrade task

Generated-by: aiautocommit
2026-03-23 08:07:06 -06:00

88 lines
2.2 KiB
Go

package app
import (
"path/filepath"
"regexp"
"testing"
"github.com/stretchr/testify/require"
)
// TestPackageJSON represents the package.json structure for testing
type PackageJSON struct {
Name string `json:"name"`
Engines struct {
Node string `json:"node"`
} `json:"engines"`
Scripts struct {
PostStart string
PreStart string
Start string
}
Dependencies map[string]string `json:"dependencies"`
DevDependencies map[string]string `json:"devDependencies"`
AllScripts map[string]string `json:"scripts"`
}
func TestApp(t *testing.T) {
app, err := NewApp("../../examples/node-bun")
require.NoError(t, err)
content, err := app.ReadFile("package.json")
require.NoError(t, err)
require.Contains(t, content, "node-bun")
var packageJSON PackageJSON
err = app.ReadJSON("package.json", &packageJSON)
require.NoError(t, err)
require.Equal(t, packageJSON.Name, "node-bun")
files, err := app.FindFiles("*.ts")
require.NoError(t, err)
require.Equal(t, len(files), 1)
require.Equal(t, files[0], "index.ts")
}
func TestAppAbsolutePath(t *testing.T) {
relPath := "../../examples/node-bun"
absPath, err := filepath.Abs(relPath)
require.NoError(t, err)
app, err := NewApp(absPath)
require.NoError(t, err)
require.Equal(t, app.Source, absPath)
}
func TestAppReadJsonWithComments(t *testing.T) {
app, err := NewApp("../../examples/config-file")
require.NoError(t, err)
var config map[string]any
err = app.ReadJSON("hello.jsonc", &config)
require.NoError(t, err)
require.Equal(t, config["hello"], "world")
}
func TestFindFilesWithContent(t *testing.T) {
app, err := NewApp("../../examples/node-bun")
require.NoError(t, err)
// Test finding files containing "process.stdout.write"
regex := regexp.MustCompile(`process\.stdout\.write`)
matches := app.FindFilesWithContent("*.ts", regex)
require.Equal(t, len(matches), 1)
require.Equal(t, matches[0], "index.ts")
// Test finding files with non-existent pattern
regex = regexp.MustCompile(`nonexistent`)
matches = app.FindFilesWithContent("*.ts", regex)
require.Empty(t, matches)
// Test with invalid glob pattern
regex = regexp.MustCompile(`test`)
matches = app.FindFilesWithContent("[invalid", regex)
require.Empty(t, matches)
}