• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2021 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package response
16
17import (
18	"bytes"
19	"reflect"
20	"testing"
21)
22
23func TestReadRspFile(t *testing.T) {
24	testCases := []struct {
25		name, in string
26		out      []string
27	}{
28		{
29			name: "single quoting test case 1",
30			in:   `./cmd '"'-C`,
31			out:  []string{"./cmd", `"-C`},
32		},
33		{
34			name: "single quoting test case 2",
35			in:   `./cmd '-C`,
36			out:  []string{"./cmd", `-C`},
37		},
38		{
39			name: "single quoting test case 3",
40			in:   `./cmd '\"'-C`,
41			out:  []string{"./cmd", `\"-C`},
42		},
43		{
44			name: "single quoting test case 4",
45			in:   `./cmd '\\'-C`,
46			out:  []string{"./cmd", `\\-C`},
47		},
48		{
49			name: "none quoting test case 1",
50			in:   `./cmd \'-C`,
51			out:  []string{"./cmd", `'-C`},
52		},
53		{
54			name: "none quoting test case 2",
55			in:   `./cmd \\-C`,
56			out:  []string{"./cmd", `\-C`},
57		},
58		{
59			name: "none quoting test case 3",
60			in:   `./cmd \"-C`,
61			out:  []string{"./cmd", `"-C`},
62		},
63		{
64			name: "double quoting test case 1",
65			in:   `./cmd "'"-C`,
66			out:  []string{"./cmd", `'-C`},
67		},
68		{
69			name: "double quoting test case 2",
70			in:   `./cmd "\\"-C`,
71			out:  []string{"./cmd", `\-C`},
72		},
73		{
74			name: "double quoting test case 3",
75			in:   `./cmd "\""-C`,
76			out:  []string{"./cmd", `"-C`},
77		},
78		{
79			name: "ninja rsp file",
80			in:   "'a'\nb\n'@'\n'foo'\\''bar'\n'foo\"bar'",
81			out:  []string{"a", "b", "@", "foo'bar", `foo"bar`},
82		},
83	}
84
85	for _, testCase := range testCases {
86		t.Run(testCase.name, func(t *testing.T) {
87			got, err := ReadRspFile(bytes.NewBuffer([]byte(testCase.in)))
88			if err != nil {
89				t.Errorf("unexpected error: %q", err)
90			}
91			if !reflect.DeepEqual(got, testCase.out) {
92				t.Errorf("expected %q got %q", testCase.out, got)
93			}
94		})
95	}
96}
97
98func TestWriteRspFile(t *testing.T) {
99	testCases := []struct {
100		name string
101		in   []string
102		out  string
103	}{
104		{
105			name: "ninja rsp file",
106			in:   []string{"a", "b", "@", "foo'bar", `foo"bar`},
107			out:  "a b '@' 'foo'\\''bar' 'foo\"bar'",
108		},
109	}
110
111	for _, testCase := range testCases {
112		t.Run(testCase.name, func(t *testing.T) {
113			buf := &bytes.Buffer{}
114			err := WriteRspFile(buf, testCase.in)
115			if err != nil {
116				t.Errorf("unexpected error: %q", err)
117			}
118			if buf.String() != testCase.out {
119				t.Errorf("expected %q got %q", testCase.out, buf.String())
120			}
121		})
122	}
123}
124