response_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. package response
  15. import (
  16. "bytes"
  17. "reflect"
  18. "testing"
  19. )
  20. func TestReadRspFile(t *testing.T) {
  21. testCases := []struct {
  22. name, in string
  23. out []string
  24. }{
  25. {
  26. name: "single quoting test case 1",
  27. in: `./cmd '"'-C`,
  28. out: []string{"./cmd", `"-C`},
  29. },
  30. {
  31. name: "single quoting test case 2",
  32. in: `./cmd '-C`,
  33. out: []string{"./cmd", `-C`},
  34. },
  35. {
  36. name: "single quoting test case 3",
  37. in: `./cmd '\"'-C`,
  38. out: []string{"./cmd", `\"-C`},
  39. },
  40. {
  41. name: "single quoting test case 4",
  42. in: `./cmd '\\'-C`,
  43. out: []string{"./cmd", `\\-C`},
  44. },
  45. {
  46. name: "none quoting test case 1",
  47. in: `./cmd \'-C`,
  48. out: []string{"./cmd", `'-C`},
  49. },
  50. {
  51. name: "none quoting test case 2",
  52. in: `./cmd \\-C`,
  53. out: []string{"./cmd", `\-C`},
  54. },
  55. {
  56. name: "none quoting test case 3",
  57. in: `./cmd \"-C`,
  58. out: []string{"./cmd", `"-C`},
  59. },
  60. {
  61. name: "double quoting test case 1",
  62. in: `./cmd "'"-C`,
  63. out: []string{"./cmd", `'-C`},
  64. },
  65. {
  66. name: "double quoting test case 2",
  67. in: `./cmd "\\"-C`,
  68. out: []string{"./cmd", `\-C`},
  69. },
  70. {
  71. name: "double quoting test case 3",
  72. in: `./cmd "\""-C`,
  73. out: []string{"./cmd", `"-C`},
  74. },
  75. {
  76. name: "ninja rsp file",
  77. in: "'a'\nb\n'@'\n'foo'\\''bar'\n'foo\"bar'",
  78. out: []string{"a", "b", "@", "foo'bar", `foo"bar`},
  79. },
  80. }
  81. for _, testCase := range testCases {
  82. t.Run(testCase.name, func(t *testing.T) {
  83. got, err := ReadRspFile(bytes.NewBuffer([]byte(testCase.in)))
  84. if err != nil {
  85. t.Errorf("unexpected error: %q", err)
  86. }
  87. if !reflect.DeepEqual(got, testCase.out) {
  88. t.Errorf("expected %q got %q", testCase.out, got)
  89. }
  90. })
  91. }
  92. }
  93. func TestWriteRspFile(t *testing.T) {
  94. testCases := []struct {
  95. name string
  96. in []string
  97. out string
  98. }{
  99. {
  100. name: "ninja rsp file",
  101. in: []string{"a", "b", "@", "foo'bar", `foo"bar`},
  102. out: "a b '@' 'foo'\\''bar' 'foo\"bar'",
  103. },
  104. }
  105. for _, testCase := range testCases {
  106. t.Run(testCase.name, func(t *testing.T) {
  107. buf := &bytes.Buffer{}
  108. err := WriteRspFile(buf, testCase.in)
  109. if err != nil {
  110. t.Errorf("unexpected error: %q", err)
  111. }
  112. if buf.String() != testCase.out {
  113. t.Errorf("expected %q got %q", testCase.out, buf.String())
  114. }
  115. })
  116. }
  117. }