|
1 | 1 | import contextlib |
| 2 | +import json |
2 | 3 | import os |
3 | 4 | import platform |
4 | 5 | import pty |
@@ -863,6 +864,138 @@ def test_report_generated(self, tmp_path, simple_test_file): |
863 | 864 | # THEN |
864 | 865 | assert "VALLOC" in output |
865 | 866 |
|
| 867 | + def test_json_generated(self, tmp_path, simple_test_file): |
| 868 | + # GIVEN |
| 869 | + results_file, _ = generate_sample_results(tmp_path, simple_test_file) |
| 870 | + json_file = tmp_path / "memray-stats-result.bin.json" |
| 871 | + |
| 872 | + # WHEN |
| 873 | + subprocess.check_output( |
| 874 | + [ |
| 875 | + sys.executable, |
| 876 | + "-m", |
| 877 | + "memray", |
| 878 | + "stats", |
| 879 | + "--json", |
| 880 | + str(results_file), |
| 881 | + ], |
| 882 | + cwd=str(tmp_path), |
| 883 | + text=True, |
| 884 | + ) |
| 885 | + |
| 886 | + # THEN |
| 887 | + assert json_file.exists() |
| 888 | + assert isinstance(json.loads(json_file.read_text()), dict) |
| 889 | + |
| 890 | + def test_json_generated_to_pretty_file_name(self, tmp_path, simple_test_file): |
| 891 | + # GIVEN |
| 892 | + orig_results_file, _ = generate_sample_results(tmp_path, simple_test_file) |
| 893 | + results_file = orig_results_file.with_name("memray-foobar.bin") |
| 894 | + orig_results_file.rename(results_file) |
| 895 | + json_file = tmp_path / "memray-stats-foobar.bin.json" |
| 896 | + |
| 897 | + # WHEN |
| 898 | + subprocess.check_output( |
| 899 | + [ |
| 900 | + sys.executable, |
| 901 | + "-m", |
| 902 | + "memray", |
| 903 | + "stats", |
| 904 | + "--json", |
| 905 | + str(results_file), |
| 906 | + ], |
| 907 | + cwd=str(tmp_path), |
| 908 | + text=True, |
| 909 | + ) |
| 910 | + |
| 911 | + # THEN |
| 912 | + assert json_file.exists() |
| 913 | + assert isinstance(json.loads(json_file.read_text()), dict) |
| 914 | + |
| 915 | + def test_json_generated_to_known_file(self, tmp_path, simple_test_file): |
| 916 | + # GIVEN |
| 917 | + results_file, _ = generate_sample_results(tmp_path, simple_test_file) |
| 918 | + json_file = tmp_path / "output.json" |
| 919 | + |
| 920 | + # WHEN |
| 921 | + subprocess.check_output( |
| 922 | + [ |
| 923 | + sys.executable, |
| 924 | + "-m", |
| 925 | + "memray", |
| 926 | + "stats", |
| 927 | + "--json", |
| 928 | + "-o", |
| 929 | + str(json_file), |
| 930 | + str(results_file), |
| 931 | + ], |
| 932 | + cwd=str(tmp_path), |
| 933 | + text=True, |
| 934 | + ) |
| 935 | + |
| 936 | + # THEN |
| 937 | + assert json_file.exists() |
| 938 | + assert isinstance(json.loads(json_file.read_text()), dict) |
| 939 | + |
| 940 | + def test_json_generated_to_existing_known_file(self, tmp_path, simple_test_file): |
| 941 | + # GIVEN |
| 942 | + results_file, _ = generate_sample_results(tmp_path, simple_test_file) |
| 943 | + json_file = tmp_path / "output.json" |
| 944 | + json_file.write_text("oops") |
| 945 | + |
| 946 | + # WHEN |
| 947 | + try: |
| 948 | + exc = None |
| 949 | + subprocess.check_output( |
| 950 | + [ |
| 951 | + sys.executable, |
| 952 | + "-m", |
| 953 | + "memray", |
| 954 | + "stats", |
| 955 | + "--json", |
| 956 | + "-o", |
| 957 | + str(json_file), |
| 958 | + str(results_file), |
| 959 | + ], |
| 960 | + cwd=str(tmp_path), |
| 961 | + stderr=subprocess.PIPE, |
| 962 | + text=True, |
| 963 | + ) |
| 964 | + except subprocess.CalledProcessError as e: |
| 965 | + exc = e |
| 966 | + |
| 967 | + # THEN |
| 968 | + assert exc is not None |
| 969 | + assert "File already exists, will not overwrite" in exc.stderr |
| 970 | + |
| 971 | + def test_json_overwrites_existing_known_file(self, tmp_path, simple_test_file): |
| 972 | + # GIVEN |
| 973 | + results_file, _ = generate_sample_results(tmp_path, simple_test_file) |
| 974 | + json_file = tmp_path / "output.json" |
| 975 | + json_file.write_text("oops") |
| 976 | + |
| 977 | + # WHEN |
| 978 | + subprocess.check_output( |
| 979 | + [ |
| 980 | + sys.executable, |
| 981 | + "-m", |
| 982 | + "memray", |
| 983 | + "stats", |
| 984 | + "--json", |
| 985 | + "--force", |
| 986 | + "--output", |
| 987 | + str(json_file), |
| 988 | + str(results_file), |
| 989 | + ], |
| 990 | + cwd=str(tmp_path), |
| 991 | + stderr=subprocess.PIPE, |
| 992 | + text=True, |
| 993 | + ) |
| 994 | + |
| 995 | + # THEN |
| 996 | + assert json_file.exists() |
| 997 | + assert isinstance(json.loads(json_file.read_text()), dict) |
| 998 | + |
866 | 999 | def test_report_detects_corrupt_input(self, tmp_path): |
867 | 1000 | # GIVEN |
868 | 1001 | bad_file = Path(tmp_path) / "badfile.bin" |
|
0 commit comments