Skip to content

Commit

Permalink
Fix FILENAME numeric string handling
Browse files Browse the repository at this point in the history
  • Loading branch information
benhoyt committed Dec 23, 2021
1 parent 952d74c commit d7a9566
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 39 deletions.
97 changes: 62 additions & 35 deletions goawk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,41 +413,7 @@ func TestCommandLine(t *testing.T) {
for _, test := range tests {
testName := strings.Join(test.args, " ")
t.Run(testName, func(t *testing.T) {
cmd := exec.Command(awkExe, test.args...)
if test.stdin != "" {
cmd.Stdin = bytes.NewReader([]byte(test.stdin))
}
errBuf := &bytes.Buffer{}
cmd.Stderr = errBuf
output, err := cmd.Output()
if err != nil {
if test.error == "" {
t.Fatalf("expected no error, got AWK error: %v (%s)", err, errBuf.String())
}
} else {
if test.error != "" {
t.Fatalf("expected AWK error, got none")
}
}
stdout := string(normalizeNewlines(output))
if stdout != test.output {
t.Fatalf("expected AWK to give %q, got %q", test.output, stdout)
}

stdout, stderr, err := runGoAWK(test.args, test.stdin)
if err != nil {
stderr = strings.TrimSpace(stderr)
if stderr != test.error {
t.Fatalf("expected GoAWK error %q, got %q", test.error, stderr)
}
} else {
if test.error != "" {
t.Fatalf("expected GoAWK error %q, got none", test.error)
}
}
if stdout != test.output {
t.Fatalf("expected GoAWK to give %q, got %q", test.output, stdout)
}
runAWKs(t, test.args, test.stdin, test.output, test.error)
})
}
}
Expand All @@ -465,6 +431,44 @@ func runGoAWK(args []string, stdin string) (stdout, stderr string, err error) {
return stdout, stderr, err
}

func runAWKs(t *testing.T, testArgs []string, testStdin, testOutput, testError string) {
cmd := exec.Command(awkExe, testArgs...)
if testStdin != "" {
cmd.Stdin = bytes.NewReader([]byte(testStdin))
}
errBuf := &bytes.Buffer{}
cmd.Stderr = errBuf
output, err := cmd.Output()
if err != nil {
if testError == "" {
t.Fatalf("expected no error, got AWK error: %v (%s)", err, errBuf.String())
}
} else {
if testError != "" {
t.Fatalf("expected AWK error, got none")
}
}
stdout := string(normalizeNewlines(output))
if stdout != testOutput {
t.Fatalf("expected AWK to give %q, got %q", testOutput, stdout)
}

stdout, stderr, err := runGoAWK(testArgs, testStdin)
if err != nil {
stderr = strings.TrimSpace(stderr)
if stderr != testError {
t.Fatalf("expected GoAWK error %q, got %q", testError, stderr)
}
} else {
if testError != "" {
t.Fatalf("expected GoAWK error %q, got none", testError)
}
}
if stdout != testOutput {
t.Fatalf("expected GoAWK to give %q, got %q", testOutput, stdout)
}
}

func TestWildcards(t *testing.T) {
if runtime.GOOS != "windows" {
// Wildcards shouldn't be expanded on non-Windows systems, and a file
Expand Down Expand Up @@ -513,6 +517,29 @@ func TestWildcards(t *testing.T) {
}
}

func TestFILENAME(t *testing.T) {
origGoAWKExe := goAWKExe
goAWKExe = "../../" + goAWKExe
defer func() { goAWKExe = origGoAWKExe }()

origDir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
err = os.Chdir("testdata/filename")
if err != nil {
t.Fatal(err)
}
defer os.Chdir(origDir)

src := `
BEGIN { FILENAME = "10"; print(FILENAME, FILENAME<2) }
BEGIN { FILENAME = 10; print(FILENAME, FILENAME<2) }
{ print(FILENAME, FILENAME<2) }
`
runAWKs(t, []string{src, "10", "10x"}, "", "10 1\n10 0\n10 0\n10x 1\n", "")
}

func normalizeNewlines(b []byte) []byte {
return bytes.Replace(b, []byte("\r\n"), []byte{'\n'}, -1)
}
6 changes: 3 additions & 3 deletions interp/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type interp struct {
nativeFuncs []nativeFunc

// File, line, and field handling
filename string
filename value
line string
lineNum int
fileLineNum int
Expand Down Expand Up @@ -977,7 +977,7 @@ func (p *interp) getVar(scope VarScope, index int) value {
case V_CONVFMT:
return str(p.convertFormat)
case V_FILENAME:
return str(p.filename)
return p.filename
case V_FS:
return str(p.fieldSep)
case V_OFMT:
Expand Down Expand Up @@ -1051,7 +1051,7 @@ func (p *interp) setVar(scope VarScope, index int, v value) error {
case V_CONVFMT:
p.convertFormat = p.toString(v)
case V_FILENAME:
p.filename = p.toString(v)
p.filename = v
case V_FS:
p.fieldSep = p.toString(v)
if utf8.RuneCountInString(p.fieldSep) > 1 {
Expand Down
2 changes: 1 addition & 1 deletion interp/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func (s byteSplitter) scan(data []byte, atEOF bool) (advance int, token []byte,

// Setup for a new input file with given name (empty string if stdin)
func (p *interp) setFile(filename string) {
p.filename = filename
p.filename = numStr(filename)
p.fileLineNum = 0
}

Expand Down
1 change: 1 addition & 0 deletions testdata/filename/10
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo
1 change: 1 addition & 0 deletions testdata/filename/10x
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar

0 comments on commit d7a9566

Please sign in to comment.