Runs attack tests and compares their outputs to a reference file.
(name, command, sample_output_file)
| 173 | @pytest.mark.parametrize("name, command, sample_output_file", attack_test_params) |
| 174 | @pytest.mark.slow |
| 175 | def test_command_line_attack(name, command, sample_output_file): |
| 176 | """Runs attack tests and compares their outputs to a reference file.""" |
| 177 | _tf_hub_tests = { |
| 178 | "interactive_mode", |
| 179 | "attack_from_transformers_adv_metrics", |
| 180 | "run_attack_hotflip_lstm_mr_4_adv_metrics", |
| 181 | } |
| 182 | if name in _tf_hub_tests and not _tensorflow_hub_available: |
| 183 | pytest.skip("tensorflow_hub is not installed") |
| 184 | # read in file and create regex |
| 185 | desired_output = open(sample_output_file, "r").read().strip() |
| 186 | print("desired_output.encoded =>", desired_output.encode()) |
| 187 | print("desired_output =>", desired_output) |
| 188 | # regex in sample file look like /.*/ |
| 189 | # / is escaped in python 3.6, but not 3.7+, so we support both |
| 190 | desired_re = ( |
| 191 | re.escape(desired_output) |
| 192 | .replace("/\\.\\/", ".") |
| 193 | .replace("/\\.\\*/", ".*") |
| 194 | .replace("\\/\\.\\*\\/", ".*") |
| 195 | ) |
| 196 | result = run_command_and_get_result(command) |
| 197 | # get output and check match |
| 198 | assert result.stdout is not None |
| 199 | stdout = result.stdout.decode().strip() |
| 200 | print("stdout.encoded =>", result.stdout) |
| 201 | print("stdout =>", stdout) |
| 202 | assert result.stderr is not None |
| 203 | stderr = result.stderr.decode().strip() |
| 204 | print("stderr =>", stderr) |
| 205 | |
| 206 | if DEBUG and not re.match(desired_re, stdout, flags=re.S): |
| 207 | pdb.set_trace() |
| 208 | assert re.match(desired_re, stdout, flags=re.S) |
| 209 | |
| 210 | assert result.returncode == 0, "return code not 0" |
nothing calls this directly
no test coverage detected