Added Windows support for the "--inplace" command flag

This commit is contained in:
Kyle Titus 2019-01-03 15:11:47 +02:00 committed by Mike Farah
parent 1159d0a212
commit 478208b7c4
3 changed files with 34 additions and 7 deletions

View File

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"runtime"
"strings" "strings"
"testing" "testing"
@ -279,7 +280,12 @@ func TestReadCmd_ErrorUnreadableFile(t *testing.T) {
if result.Error == nil { if result.Error == nil {
t.Error("Expected command to fail due to unknown file") t.Error("Expected command to fail due to unknown file")
} }
expectedOutput := `open fake-unknown: no such file or directory` var expectedOutput string
if runtime.GOOS == "windows" {
expectedOutput = `open fake-unknown: The system cannot find the file specified.`
} else {
expectedOutput = `open fake-unknown: no such file or directory`
}
assertResult(t, expectedOutput, result.Error.Error()) assertResult(t, expectedOutput, result.Error.Error())
} }
@ -493,7 +499,12 @@ func TestPrefixCmd_ErrorUnreadableFile(t *testing.T) {
if result.Error == nil { if result.Error == nil {
t.Error("Expected command to fail due to unknown file") t.Error("Expected command to fail due to unknown file")
} }
expectedOutput := `open fake-unknown: no such file or directory` var expectedOutput string
if runtime.GOOS == "windows" {
expectedOutput = `open fake-unknown: The system cannot find the file specified.`
} else {
expectedOutput = `open fake-unknown: no such file or directory`
}
assertResult(t, expectedOutput, result.Error.Error()) assertResult(t, expectedOutput, result.Error.Error())
} }
@ -693,7 +704,12 @@ func TestWriteCmd_ErrorUnreadableFile(t *testing.T) {
if result.Error == nil { if result.Error == nil {
t.Error("Expected command to fail due to unknown file") t.Error("Expected command to fail due to unknown file")
} }
expectedOutput := `open fake-unknown: no such file or directory` var expectedOutput string
if runtime.GOOS == "windows" {
expectedOutput = `open fake-unknown: The system cannot find the file specified.`
} else {
expectedOutput = `open fake-unknown: no such file or directory`
}
assertResult(t, expectedOutput, result.Error.Error()) assertResult(t, expectedOutput, result.Error.Error())
} }
@ -1019,7 +1035,12 @@ func TestMergeCmd_ErrorUnreadableFile(t *testing.T) {
if result.Error == nil { if result.Error == nil {
t.Error("Expected command to fail due to unknown file") t.Error("Expected command to fail due to unknown file")
} }
expectedOutput := `Error updating document at index 0: open fake-unknown: no such file or directory` var expectedOutput string
if runtime.GOOS == "windows" {
expectedOutput = `Error updating document at index 0: open fake-unknown: The system cannot find the file specified.`
} else {
expectedOutput = `Error updating document at index 0: open fake-unknown: no such file or directory`
}
assertResult(t, expectedOutput, result.Error.Error()) assertResult(t, expectedOutput, result.Error.Error())
} }

4
yq.go
View File

@ -462,11 +462,11 @@ func readAndUpdate(stdOut io.Writer, inputFile string, updateData updateDataFn)
if err != nil { if err != nil {
return err return err
} }
err = tempFile.Chmod(info.Mode()) destinationName = tempFile.Name()
err = os.Chmod(destinationName, info.Mode())
if err != nil { if err != nil {
return err return err
} }
destinationName = tempFile.Name()
destination = tempFile destination = tempFile
defer func() { defer func() {
safelyCloseFile(tempFile) safelyCloseFile(tempFile)

View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"runtime"
"testing" "testing"
) )
@ -64,6 +65,11 @@ func TestNewYaml_WithUnknownScript(t *testing.T) {
if err == nil { if err == nil {
t.Error("Expected error due to unknown file") t.Error("Expected error due to unknown file")
} }
expectedOutput := `open fake-unknown: no such file or directory` var expectedOutput string
if runtime.GOOS == "windows" {
expectedOutput = `open fake-unknown: The system cannot find the file specified.`
} else {
expectedOutput = `open fake-unknown: no such file or directory`
}
assertResult(t, expectedOutput, err.Error()) assertResult(t, expectedOutput, err.Error())
} }