Debian Codegraph @

build/qualification_test/src/suite_cli/test_repair.rs

raw symbols SCIP
1 use super::fixture::FixtureCli;
2 use std::path::PathBuf;
3 use std::process::Command;
4
5 /// A switch to distinguish test and repair modes
6 #[derive(Debug, Copy, Clone, PartialEq)]
7 enum Repair {
8 TestOnly,
9 TestAndRepair,
10 }
11
12 /// A string containing valid json format but the crystal-facet-uml model is damaged
13 static JSON_BAD_CONTENT: &'static str = "\
14 {
15 \"head\": {
16 \"encoding\": \"utf-8\"
17 }
18 ,
19 \"views\":
20 [
21 ]
22 ,
23 \"nodes\":
24 [
25 {
26 \"classifier\": {
27 \"id\": 1,
28 \"main_type\": 121,
29 \"type\": \"Component\",
30 \"stereotype\": \"subsystem\",
31 \"name\": \"AngleSteering\",
32 \"uuid\": \"8190a2ed-df2e-4427-8ebb-0272cd62149a\",
33 \"features\":
34 [
35 ]
36 }
37 }
38 ]
39 ,
40 \"edges\":
41 [
42 ]
43 }
44 ";
45
46 /// Test that creating an sqlite3 based format is possible and can be repaired.
47 ///
48 /// # Arguments
49 ///
50 /// * `environment` - A test fixture stating the test environment
51 ///
52 /// # Errors
53 ///
54 /// This function returns `Err()` if the test case produced an unexpected result
55 ///
56 /// # Panics
57 ///
58 /// This function panics if the test environment causes errors.
59 ///
60 pub(super) fn testcase_repair_new_cfu1(environment: &mut FixtureCli) -> Result<(), ()> {
61 test_repair_or_test(
62 Repair::TestAndRepair,
63 "sqlite3_db.cfu1",
64 "Errors found: 1",
65 environment,
66 )
67 }
68
69 /// Test that creating an sqlite3 based format is not done with -t option.
70 ///
71 /// # Arguments
72 ///
73 /// * `environment` - A test fixture stating the test environment
74 ///
75 /// # Errors
76 ///
77 /// This function returns `Err()` if the test case produced an unexpected result
78 ///
79 /// # Panics
80 ///
81 /// This function panics if the test environment causes errors.
82 ///
83 pub(super) fn testcase_test_new_cfu1(environment: &mut FixtureCli) -> Result<(), ()> {
84 test_repair_or_test(
85 Repair::TestOnly,
86 "sqlite3_db.cfu1",
87 "Errors found: 1",
88 environment,
89 )
90 }
91
92 /// Test that repairing an existing, writeable json file is possible.
93 ///
94 /// # Arguments
95 ///
96 /// * `environment` - A test fixture stating the test environment
97 ///
98 /// # Errors
99 ///
100 /// This function returns `Err()` if the test case produced an unexpected result
101 ///
102 /// # Panics
103 ///
104 /// This function panics if the test environment causes errors.
105 ///
106 pub(super) fn testcase_repair_json_rw(environment: &mut FixtureCli) -> Result<(), ()> {
107 environment.create_file(
108 "test.cfuJ",
109 JSON_BAD_CONTENT,
110 super::fixture::Permissions::ReadWrite,
111 );
112 test_repair_or_test(
113 Repair::TestAndRepair,
114 "test.cfuJ",
115 "Errors fixed: 1",
116 environment,
117 )
118 }
119
120 /// Test that testing an existing, read-only json file is possible.
121 ///
122 /// # Arguments
123 ///
124 /// * `environment` - A test fixture stating the test environment
125 ///
126 /// # Errors
127 ///
128 /// This function returns `Err()` if the test case produced an unexpected result
129 ///
130 /// # Panics
131 ///
132 /// This function panics if the test environment causes errors.
133 ///
134 pub(super) fn testcase_test_json_ro(environment: &mut FixtureCli) -> Result<(), ()> {
135 environment.create_file(
136 "test.cfuJ",
137 JSON_BAD_CONTENT,
138 super::fixture::Permissions::ReadOnly,
139 );
140 test_repair_or_test(
141 Repair::TestOnly,
142 "test.cfuJ",
143 "Errors found: 2",
144 environment,
145 )
146 }
147
148 /// Test that creating an sqlite3 based format is possible and can be repaired.
149 ///
150 /// # Arguments
151 ///
152 /// * `repair` - Indicator if the test case shall repair or only test the database
153 /// * `file_name` - Filename to use for test case
154 /// * `expected_output` - String that is expected to be contained in the output
155 /// * `environment` - A test fixture stating the test environment
156 ///
157 /// # Errors
158 ///
159 /// This function returns `Err()` if the test case produced an unexpected result
160 ///
161 /// # Panics
162 ///
163 /// This function panics if the test environment causes errors.
164 ///
165 fn test_repair_or_test(
166 repair: Repair,
167 file_name: &str,
168 expected_output: &str,
169 environment: &mut FixtureCli,
170 ) -> Result<(), ()> {
171 /* create the db_to_use_param string, panic in case an error occured */
172 let mut db_to_use = PathBuf::from(environment.temp_dir);
173 db_to_use.push(file_name);
174 let db_to_use_param = db_to_use.into_os_string().into_string().unwrap();
175
176 let repair_param: &str = match repair {
177 Repair::TestOnly => "-t",
178 Repair::TestAndRepair => "-r",
179 };
180 print!(
181 "call: {} {} {}\n",
182 environment.exe_to_test, repair_param, db_to_use_param
183 );
184 let output = Command::new(environment.exe_to_test)
185 .args(&[repair_param, &db_to_use_param])
186 .output()
187 .expect("Err at running process");
188
189 let stdout = match std::string::String::from_utf8(output.stdout) {
190 Ok(stdout) => stdout,
191 Err(_err) => return Result::Err(()),
192 };
193
194 /* check if the returned string looks as expected */
195 /* 1 error should be found which is the missing root diagram */
196 let as_expected = stdout.contains(expected_output);
197
198 /* check that the exit code is 0 */
199 let exit_ok: bool = output.status.success();
200
201 print!("stdout: <<{}>>:{}\n", stdout, stdout.len());
202 if as_expected && !exit_ok {
203 Result::Ok(())
204 } else {
205 Result::Err(())
206 }
207 }
208
209 /*
210 Copyright 2022-2026 Andreas Warnke
211
212 Licensed under the Apache License, Version 2.0 (the "License");
213 you may not use this file except in compliance with the License.
214 You may obtain a copy of the License at
215
216 http://www.apache.org/licenses/LICENSE-2.0
217
218 Unless required by applicable law or agreed to in writing, software
219 distributed under the License is distributed on an "AS IS" BASIS,
220 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
221 See the License for the specific language governing permissions and
222 limitations under the License.
223 */