Used when `REWRITE_TESTS` mode is enabled. Currently, the implementation of the rewrite functionality is inefficient. 1) The testStatements vector container does not contain all the tests specified in the file. Rather, it only contains tests for a single CASE. See `parseAndRegisterFileGroup()`. When rewriting the output, we need to first find this CASE. 2) This function will be called from `TearD
| 125 | // parallel. The current code does not handle calls for the same test file in parallel, and |
| 126 | // rewrite mode is expected to be run in single-threaded mode using `TEST_JOBS=1`. |
| 127 | void rewriteTestFile() const { |
| 128 | std::fstream file; |
| 129 | std::string newFile; |
| 130 | std::string currLine; |
| 131 | std::string testCaseName; |
| 132 | file.open(testPath); |
| 133 | |
| 134 | for (auto& statement : testStatements) { |
| 135 | if (statement.query.empty() || statement.isPartOfStatementBlock) { |
| 136 | continue; |
| 137 | } |
| 138 | // Find `statement` in the file. |
| 139 | while (getline(file, currLine)) { |
| 140 | if (!currLine.starts_with("-STATEMENT")) { |
| 141 | newFile += currLine + '\n'; |
| 142 | if (currLine.starts_with("-CASE")) { |
| 143 | static constexpr size_t caseNameOffset = std::string_view("-CASE ").size(); |
| 144 | testCaseName = currLine.substr(caseNameOffset); |
| 145 | } |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | newFile += currLine + '\n'; |
| 150 | |
| 151 | if (testCaseName != statement.testCaseName) { |
| 152 | // Not the CASE for the current `statement`. |
| 153 | continue; |
| 154 | } |
| 155 | |
| 156 | std::string stmt = currLine; |
| 157 | while (getline(file, currLine)) { |
| 158 | if (currLine.starts_with("----")) { |
| 159 | // "----" indicates the end of a statement. |
| 160 | break; |
| 161 | } |
| 162 | newFile += currLine + '\n'; |
| 163 | // There may be other test options before the output is specified. |
| 164 | if (!currLine.starts_with("#") && !currLine.starts_with("-")) { |
| 165 | stmt += currLine; |
| 166 | } |
| 167 | } |
| 168 | // Manually add `connName` as `statement.query` does not retain it, if any. |
| 169 | std::string connName; |
| 170 | if (statement.connName.has_value() && |
| 171 | statement.connName.value() != "conn_default") { |
| 172 | connName = "[" + statement.connName.value() + "] "; |
| 173 | } |
| 174 | |
| 175 | if (removeAllSpaces(stmt) == |
| 176 | removeAllSpaces("-STATEMENT " + connName + statement.originalQuery)) { |
| 177 | // Found the line containing `statement`. |
| 178 | break; |
| 179 | } |
| 180 | |
| 181 | newFile += currLine + '\n'; |
| 182 | } |
| 183 | |
| 184 | // For all cases, ignore the specified expected output in the test file and append the |