mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-05 20:15:36 +00:00
Compare commits
5 Commits
c5cbf9760b
...
11f4dc1a03
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
11f4dc1a03 | ||
|
|
0f4fb8d35e | ||
|
|
80c319aa0c | ||
|
|
b25ae78545 | ||
|
|
b151522485 |
@ -101,12 +101,15 @@ func evaluateAll(cmd *cobra.Command, args []string) (cmdError error) {
|
||||
}
|
||||
|
||||
if frontMatter != "" {
|
||||
originalFilename := args[0]
|
||||
frontMatterHandler := yqlib.NewFrontMatterHandler(args[0])
|
||||
err = frontMatterHandler.Split()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
args[0] = frontMatterHandler.GetYamlFrontMatterFilename()
|
||||
yqlib.SetFilenameAlias(args[0], originalFilename)
|
||||
defer yqlib.ClearFilenameAliases()
|
||||
|
||||
if frontMatter == "process" {
|
||||
reader := frontMatterHandler.GetContentReader()
|
||||
|
||||
@ -13,6 +13,7 @@ func TestCreateEvaluateAllCommand(t *testing.T) {
|
||||
|
||||
if cmd == nil {
|
||||
t.Fatal("createEvaluateAllCommand returned nil")
|
||||
return
|
||||
}
|
||||
|
||||
// Test basic command properties
|
||||
|
||||
@ -122,12 +122,15 @@ func evaluateSequence(cmd *cobra.Command, args []string) (cmdError error) {
|
||||
|
||||
if frontMatter != "" {
|
||||
yqlib.GetLogger().Debug("using front matter handler")
|
||||
originalFilename := args[0]
|
||||
frontMatterHandler := yqlib.NewFrontMatterHandler(args[0])
|
||||
err = frontMatterHandler.Split()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
args[0] = frontMatterHandler.GetYamlFrontMatterFilename()
|
||||
yqlib.SetFilenameAlias(args[0], originalFilename)
|
||||
defer yqlib.ClearFilenameAliases()
|
||||
|
||||
if frontMatter == "process" {
|
||||
reader := frontMatterHandler.GetContentReader()
|
||||
|
||||
@ -13,6 +13,7 @@ func TestCreateEvaluateSequenceCommand(t *testing.T) {
|
||||
|
||||
if cmd == nil {
|
||||
t.Fatal("createEvaluateSequenceCommand returned nil")
|
||||
return
|
||||
}
|
||||
|
||||
// Test basic command properties
|
||||
|
||||
@ -195,6 +195,7 @@ func TestNew(t *testing.T) {
|
||||
|
||||
if rootCmd == nil {
|
||||
t.Fatal("New() returned nil")
|
||||
return
|
||||
}
|
||||
|
||||
// Test basic command properties
|
||||
|
||||
@ -11,7 +11,7 @@ var (
|
||||
GitDescribe string
|
||||
|
||||
// Version is main version number that is being run at the moment.
|
||||
Version = "v4.52.4"
|
||||
Version = "v4.52.5"
|
||||
|
||||
// VersionPrerelease is a pre-release marker for the version. If this is "" (empty string)
|
||||
// then it means that it is a final release. Otherwise, this is a pre-release
|
||||
|
||||
@ -54,3 +54,25 @@ func TestAllAtOnceEvaluateNodes(t *testing.T) {
|
||||
test.AssertResultComplex(t, tt.expected, resultsToString(t, list))
|
||||
}
|
||||
}
|
||||
|
||||
func TestTomlDecoderCanBeReinitializedAcrossDocuments(t *testing.T) {
|
||||
decoder := NewTomlDecoder()
|
||||
|
||||
firstDocuments, err := ReadDocuments(strings.NewReader("id = \"Foobar\"\n"), decoder)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read first TOML document: %v", err)
|
||||
}
|
||||
if firstDocuments.Len() != 1 {
|
||||
t.Fatalf("expected first document count to be 1, got %d", firstDocuments.Len())
|
||||
}
|
||||
test.AssertResult(t, "Foobar", firstDocuments.Front().Value.(*CandidateNode).Content[1].Value)
|
||||
|
||||
secondDocuments, err := ReadDocuments(strings.NewReader("id = \"Banana\"\n"), decoder)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read second TOML document: %v", err)
|
||||
}
|
||||
if secondDocuments.Len() != 1 {
|
||||
t.Fatalf("expected second document count to be 1, got %d", secondDocuments.Len())
|
||||
}
|
||||
test.AssertResult(t, "Banana", secondDocuments.Front().Value.(*CandidateNode).Content[1].Value)
|
||||
}
|
||||
|
||||
@ -44,6 +44,7 @@ func (dec *tomlDecoder) Init(reader io.Reader) error {
|
||||
}
|
||||
dec.pendingComments = make([]string, 0)
|
||||
dec.firstContentSeen = false
|
||||
dec.finished = false
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@ -95,6 +95,7 @@ func TestParserSingleOperation(t *testing.T) {
|
||||
test.AssertResultComplex(t, nil, err)
|
||||
if result == nil {
|
||||
t.Fatal("Expected non-nil result for single operation")
|
||||
return
|
||||
}
|
||||
if result.Operation == nil {
|
||||
t.Fatal("Expected operation to be set")
|
||||
|
||||
@ -108,6 +108,55 @@ yaml: doc
|
||||
fmHandler.CleanUp()
|
||||
}
|
||||
|
||||
func TestFrontMatterFilenamePreserved(t *testing.T) {
|
||||
// Regression test for https://github.com/mikefarah/yq/issues/2538
|
||||
// When using --front-matter, the filename operator should return
|
||||
// the original filename, not the path to the temporary file.
|
||||
file := createTestFile(`---
|
||||
name: john
|
||||
---
|
||||
Some content
|
||||
`)
|
||||
originalFilename := "/path/to/original/file.md"
|
||||
|
||||
fmHandler := NewFrontMatterHandler(file)
|
||||
err := fmHandler.Split()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
tempFilename := fmHandler.GetYamlFrontMatterFilename()
|
||||
|
||||
// Register the alias (as the command code does)
|
||||
SetFilenameAlias(tempFilename, originalFilename)
|
||||
defer ClearFilenameAliases()
|
||||
|
||||
// Verify resolveFilename returns the original name
|
||||
resolved := resolveFilename(tempFilename)
|
||||
test.AssertResult(t, originalFilename, resolved)
|
||||
|
||||
// Read documents using the temp file, verify they get the original filename
|
||||
reader, err := readStream(tempFilename)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
decoder := NewYamlDecoder(ConfiguredYamlPreferences)
|
||||
docs, err := readDocuments(reader, tempFilename, 0, decoder)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if docs.Len() == 0 {
|
||||
t.Fatal("expected at least one document")
|
||||
}
|
||||
|
||||
firstDoc := docs.Front().Value.(*CandidateNode)
|
||||
test.AssertResult(t, originalFilename, firstDoc.filename)
|
||||
|
||||
tryRemoveTempFile(file)
|
||||
fmHandler.CleanUp()
|
||||
}
|
||||
|
||||
func TestFrontMatterSplitWithArray(t *testing.T) {
|
||||
file := createTestFile(`[1,2,3]
|
||||
---
|
||||
|
||||
@ -76,6 +76,7 @@ func (s *streamEvaluator) EvaluateFiles(expression string, filenames []string, p
|
||||
}
|
||||
|
||||
func (s *streamEvaluator) Evaluate(filename string, reader io.Reader, node *ExpressionNode, printer Printer, decoder Decoder) (uint, error) {
|
||||
filename = resolveFilename(filename)
|
||||
|
||||
var currentIndex uint
|
||||
err := decoder.Init(reader)
|
||||
|
||||
@ -9,6 +9,29 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// filenameAliases maps real file paths to display names.
|
||||
// Used by front matter handling to preserve original filenames
|
||||
// when the actual content is read from temporary files.
|
||||
var filenameAliases = map[string]string{}
|
||||
|
||||
// SetFilenameAlias registers a display name for a file path so that
|
||||
// the filename operator returns the original name instead of a temp path.
|
||||
func SetFilenameAlias(realPath string, displayName string) {
|
||||
filenameAliases[realPath] = displayName
|
||||
}
|
||||
|
||||
// ClearFilenameAliases removes all filename aliases.
|
||||
func ClearFilenameAliases() {
|
||||
filenameAliases = map[string]string{}
|
||||
}
|
||||
|
||||
func resolveFilename(filename string) string {
|
||||
if alias, ok := filenameAliases[filename]; ok {
|
||||
return alias
|
||||
}
|
||||
return filename
|
||||
}
|
||||
|
||||
func readStream(filename string) (io.Reader, error) {
|
||||
var reader *bufio.Reader
|
||||
if filename == "-" {
|
||||
@ -36,6 +59,7 @@ func ReadDocuments(reader io.Reader, decoder Decoder) (*list.List, error) {
|
||||
}
|
||||
|
||||
func readDocuments(reader io.Reader, filename string, fileIndex int, decoder Decoder) (*list.List, error) {
|
||||
filename = resolveFilename(filename)
|
||||
err := decoder.Init(reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@ -1,3 +1,9 @@
|
||||
4.52.5:
|
||||
- Fix: reset TOML decoder state between files (#2634) thanks @terminalchai
|
||||
- Fix: preserve original filename when using --front-matter (#2613) thanks @cobyfrombrooklyn-bot
|
||||
- Fix typo in filename (#2611) thanks @alexandear
|
||||
- Bumped dependencies
|
||||
|
||||
4.52.4:
|
||||
- Dropping windows/arm - no longer supported in cross-compile
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
name: yq
|
||||
version: 'v4.52.4'
|
||||
version: 'v4.52.5'
|
||||
summary: A lightweight and portable command-line data file processor
|
||||
description: |
|
||||
`yq` uses [jq](https://github.com/stedolan/jq) like syntax but works with yaml, json, xml, csv, properties and TOML files.
|
||||
@ -32,6 +32,6 @@ parts:
|
||||
build-environment:
|
||||
- CGO_ENABLED: 0
|
||||
source: https://github.com/mikefarah/yq.git
|
||||
source-tag: v4.52.4
|
||||
source-tag: v4.52.5
|
||||
build-snaps:
|
||||
- go/latest/stable
|
||||
|
||||
@ -14,6 +14,7 @@ func TestMainFunction(t *testing.T) {
|
||||
cmd := command.New()
|
||||
if cmd == nil {
|
||||
t.Fatal("command.New() returned nil")
|
||||
return
|
||||
}
|
||||
|
||||
if cmd.Use != "yq" {
|
||||
@ -99,6 +100,7 @@ func TestMainFunctionExecution(t *testing.T) {
|
||||
// the command structure is correct and can be configured
|
||||
if cmd == nil {
|
||||
t.Fatal("Command should not be nil")
|
||||
return
|
||||
}
|
||||
|
||||
if cmd.Use != "yq" {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user