-
Notifications
You must be signed in to change notification settings - Fork 541
Capture stderr in regression case logs #3471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RanaPriyansh
wants to merge
3
commits into
OpenFAST:dev
Choose a base branch
from
RanaPriyansh:fix/3448-stderr-test
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+161
−2
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| import os | ||
| from pathlib import Path | ||
| import shlex | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| import unittest | ||
|
|
||
| import openfastDrivers | ||
|
|
||
|
|
||
| class OpenfastDriversTest(unittest.TestCase): | ||
| def setUp(self): | ||
| self.temp_dir = tempfile.TemporaryDirectory() | ||
| self.case_dir = Path(self.temp_dir.name) / "case" | ||
| self.case_dir.mkdir() | ||
| self.fixture = self.case_dir / "fixture.py" | ||
| self.fixture.write_text( | ||
| "import pathlib\n" | ||
| "import os\n" | ||
| "import sys\n" | ||
| "print('stdout marker')\n" | ||
| "print('stderr marker', file=sys.stderr)\n" | ||
| "pathlib.Path(__file__).with_name('arguments.txt').write_text('\\n'.join(sys.argv[1:]))\n" | ||
| "raise SystemExit(int(os.environ['OPENFAST_FIXTURE_EXIT_CODE']))\n" | ||
| ) | ||
|
|
||
| def tearDown(self): | ||
| self.temp_dir.cleanup() | ||
|
|
||
| def invoke(self, *, exit_code=0, mode="normal", extra_flags=""): | ||
| result = subprocess.run( | ||
| [ | ||
| sys.executable, | ||
| str(Path(__file__).resolve()), | ||
| "--invoke", | ||
| mode, | ||
| str(exit_code), | ||
| self.fixture.name, | ||
| extra_flags, | ||
| ], | ||
| cwd=self.case_dir, | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.PIPE, | ||
| text=True, | ||
| check=False, | ||
| ) | ||
| return result.returncode, result.stdout, result.stderr | ||
|
|
||
| def test_normal_case_preserves_exit_code_flags_stdout_and_overwrite(self): | ||
| for exit_code in (0, 7): | ||
| with self.subTest(exit_code=exit_code): | ||
| log_file = self.case_dir / "case.log" | ||
| log_file.write_text("old log\n") | ||
|
|
||
| return_code, output, errors = self.invoke(exit_code=exit_code, extra_flags="extra flag") | ||
|
|
||
| self.assertEqual(return_code, exit_code) | ||
| self.assertNotIn("stdout marker", output) | ||
| self.assertNotIn("stdout marker", errors) | ||
| log = log_file.read_text() | ||
| self.assertIn("stdout marker", log) | ||
| self.assertNotIn("old log", log) | ||
| arguments = (self.case_dir / "arguments.txt").read_text().splitlines() | ||
| self.assertEqual(arguments, ["input.fst", "extra", "flag"]) | ||
|
|
||
| def test_normal_case_captures_stderr_and_hides_child_streams(self): | ||
| for exit_code in (0, 7): | ||
| with self.subTest(exit_code=exit_code): | ||
| log_file = self.case_dir / "case.log" | ||
| log_file.write_text("old log\n") | ||
|
|
||
| return_code, output, errors = self.invoke(exit_code=exit_code, extra_flags="extra flag") | ||
|
|
||
| self.assertEqual(return_code, exit_code) | ||
| self.assertIn("stderr marker", log_file.read_text()) | ||
| self.assertNotIn("stderr marker", output) | ||
| self.assertNotIn("stderr marker", errors) | ||
|
|
||
| def test_restart_case_preserves_exit_code_arguments_stdout_and_overwrite(self): | ||
| for exit_code in (0, 7): | ||
| with self.subTest(exit_code=exit_code): | ||
| log_file = self.case_dir / "case_2.log" | ||
| log_file.write_text("old log\n") | ||
| return_code, output, errors = self.invoke(exit_code=exit_code, mode="restart") | ||
|
|
||
| self.assertEqual(return_code, exit_code) | ||
| self.assertNotIn("stdout marker", output) | ||
| self.assertNotIn("stdout marker", errors) | ||
| log = log_file.read_text() | ||
| self.assertIn("stdout marker", log) | ||
| self.assertNotIn("old log", log) | ||
| self.assertEqual( | ||
| (self.case_dir / "arguments.txt").read_text().splitlines(), | ||
| ["-restart", "input"], | ||
| ) | ||
|
|
||
| def test_restart_case_captures_stderr_and_hides_child_streams(self): | ||
| for exit_code in (0, 7): | ||
| with self.subTest(exit_code=exit_code): | ||
| log_file = self.case_dir / "case_2.log" | ||
| log_file.write_text("old log\n") | ||
| return_code, output, errors = self.invoke(exit_code=exit_code, mode="restart") | ||
|
|
||
| self.assertEqual(return_code, exit_code) | ||
| self.assertIn("stderr marker", log_file.read_text()) | ||
| self.assertNotIn("stderr marker", output) | ||
| self.assertNotIn("stderr marker", errors) | ||
|
|
||
| def test_verbose_case_preserves_streams_arguments_and_log_state(self): | ||
| log_file = self.case_dir / "case.log" | ||
| for existing_log in (True, False): | ||
| for exit_code in (0, 7): | ||
| with self.subTest(existing_log=existing_log, exit_code=exit_code): | ||
| if existing_log: | ||
| log_file.write_text("old log\n") | ||
| elif log_file.exists(): | ||
| log_file.unlink() | ||
|
|
||
| return_code, output, errors = self.invoke(exit_code=exit_code, mode="verbose", extra_flags="extra") | ||
|
|
||
| self.assertEqual(return_code, exit_code) | ||
| self.assertIn("stdout marker", output) | ||
| self.assertIn("stderr marker", errors) | ||
| self.assertNotIn("stderr marker", output) | ||
| self.assertNotIn("stdout marker", errors) | ||
| self.assertEqual(log_file.exists(), existing_log) | ||
| if existing_log: | ||
| self.assertEqual(log_file.read_text(), "old log\n") | ||
| self.assertEqual( | ||
| (self.case_dir / "arguments.txt").read_text().splitlines(), | ||
| ["input.fst", "extra"], | ||
| ) | ||
|
|
||
|
|
||
| def invoke_fixture(mode, exit_code, fixture, extra_flags): | ||
| os.environ["OPENFAST_FIXTURE_EXIT_CODE"] = str(exit_code) | ||
| if os.name == "nt": | ||
| executable = subprocess.list2cmdline([sys.executable, fixture]) | ||
| else: | ||
| executable = " ".join((shlex.quote(sys.executable), shlex.quote(fixture))) | ||
| log_file = {"normal": "case.log", "restart": "case_2.log"}.get(mode) | ||
| return openfastDrivers._runCase( | ||
| executable, | ||
| "input.fst", | ||
| log_file, | ||
| sys.stdout, | ||
| restart=mode in ("restart", "verbose"), | ||
| ExtraFlags=extra_flags, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| if len(sys.argv) > 1 and sys.argv[1] == "--invoke": | ||
| sys.exit(invoke_fixture(*sys.argv[2:])) | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.