|
| 1 | +import os |
| 2 | +import json |
| 3 | +import subprocess |
| 4 | +import pytest |
| 5 | + |
| 6 | +from typing import Set, Optional, List |
| 7 | + |
| 8 | +GH_INTEGRATION_TESTS_TOKEN = os.environ["GH_INTEGRATION_TESTS_TOKEN"] |
| 9 | + |
| 10 | +def run_src_fingerprint(provider: str, args: Optional[List[str]] = []): |
| 11 | + subprocess.run( |
| 12 | + [ |
| 13 | + "./src-fingerprint", |
| 14 | + "-p", |
| 15 | + provider, |
| 16 | + "--token", |
| 17 | + GH_INTEGRATION_TESTS_TOKEN, |
| 18 | + "-f", |
| 19 | + "jsonl", |
| 20 | + "-o", |
| 21 | + "fingerprints.jsonl" |
| 22 | + ] + args, |
| 23 | + check=True |
| 24 | + ) |
| 25 | + |
| 26 | +def load_jsonl(jsonl_path): |
| 27 | + with open(jsonl_path) as f: |
| 28 | + for line in f: |
| 29 | + yield json.loads(line) |
| 30 | + |
| 31 | +def get_output_repos(output_path) -> Set[str]: |
| 32 | + return {x["repository_name"] for x in load_jsonl(output_path)} |
| 33 | + |
| 34 | + |
| 35 | +def test_local_repository(): |
| 36 | + run_src_fingerprint(provider="repository", args=["--object", "."]) |
| 37 | + repos = get_output_repos("fingerprints.jsonl") |
| 38 | + os.remove("fingerprints.jsonl") |
| 39 | + assert len(repos) == 1 |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize( |
| 43 | + "title, cmd_args, expected_output_repos", [ |
| 44 | + ( |
| 45 | + "Get all private repos accesible to user gg-src-fingerprint except archived ones", |
| 46 | + [], |
| 47 | + { |
| 48 | + # Repos for user gg-src-fingerprint |
| 49 | + "main_private", |
| 50 | + # Repos for org gg-src-fingerprint-org |
| 51 | + "external_private", |
| 52 | + "gg_src_fingerprint_private", |
| 53 | + } |
| 54 | + ), |
| 55 | + ( |
| 56 | + "Get all private repos accesible to user gg-src-fingerprint", |
| 57 | + ["--include-archived-repos"], |
| 58 | + { |
| 59 | + # Repos for user gg-src-fingerprint |
| 60 | + "main_private_archive", |
| 61 | + "main_private", |
| 62 | + # Repos for org gg-src-fingerprint-org |
| 63 | + "gg_src_fingerprint_private", |
| 64 | + "gg_src_fingerprint_private_archive", |
| 65 | + "external_private", |
| 66 | + } |
| 67 | + ), |
| 68 | + ( |
| 69 | + "Get all repos accesible to user gg-src-fingerprint except archived ones", |
| 70 | + ["--include-public-repos", "--include-forked-repos"], |
| 71 | + { |
| 72 | + # Repos for user gg-src-fingerprint |
| 73 | + "main_private", |
| 74 | + "main_public", |
| 75 | + "src-fingerprint", |
| 76 | + # Repos for org gg-src-fingerprint-org |
| 77 | + "external_private", |
| 78 | + "gg_src_fingerprint_private", |
| 79 | + "gg_src_fingerprint_public", |
| 80 | + } |
| 81 | + ), |
| 82 | + ( |
| 83 | + "Get all repos accesible to user gg-src-fingerprint except forked ones", |
| 84 | + ["--include-public-repos", "--include-archived-repos"], |
| 85 | + { |
| 86 | + # Repos for user gg-src-fingerprint |
| 87 | + "main_archive", |
| 88 | + "main_private", |
| 89 | + "main_private_archive", |
| 90 | + "main_public", |
| 91 | + # Repos for org gg-src-fingerprint-org |
| 92 | + "external_private", |
| 93 | + "gg_src_fingerprint_private", |
| 94 | + "gg_src_fingerprint_private_archive", |
| 95 | + "gg_src_fingerprint_public", |
| 96 | + "gg_src_fingerprint_public_archive", |
| 97 | + } |
| 98 | + ), |
| 99 | + ( |
| 100 | + "Get all repos accesible to user gg-src-fingerprint", |
| 101 | + ["--include-public-repos", "--include-archived-repos", "--include-forked-repos"], |
| 102 | + { |
| 103 | + # Repos for user gg-src-fingerprint |
| 104 | + "main_archive", |
| 105 | + "main_private", |
| 106 | + "main_private_archive", |
| 107 | + "main_public", |
| 108 | + "src-fingerprint", |
| 109 | + # Repos for org gg-src-fingerprint-org |
| 110 | + "external_private", |
| 111 | + "gg_src_fingerprint_private", |
| 112 | + "gg_src_fingerprint_private_archive", |
| 113 | + "gg_src_fingerprint_public", |
| 114 | + "gg_src_fingerprint_public_archive", |
| 115 | + } |
| 116 | + ) |
| 117 | + ] |
| 118 | +) |
| 119 | +def test_src_fingerprint_no_object_specified(title, cmd_args, expected_output_repos): |
| 120 | + run_src_fingerprint(provider="github", args=cmd_args) |
| 121 | + output_repos = get_output_repos("fingerprints.jsonl") |
| 122 | + os.remove("fingerprints.jsonl") |
| 123 | + assert output_repos == expected_output_repos |
| 124 | + |
| 125 | + |
| 126 | +@pytest.mark.parametrize( |
| 127 | + "title, cmd_args, expected_output_repos", [ |
| 128 | + ( |
| 129 | + "Get all private repos except archived ones for org gg-src-fingerprint-org", |
| 130 | + [], |
| 131 | + { |
| 132 | + "gg_src_fingerprint_private", |
| 133 | + "external_private" |
| 134 | + } |
| 135 | + ), |
| 136 | + ( |
| 137 | + "Get all private repos for org gg-src-fingerprint-org", |
| 138 | + ["--include-archived-repos"], |
| 139 | + { |
| 140 | + "gg_src_fingerprint_private", |
| 141 | + "gg_src_fingerprint_private_archive", |
| 142 | + "external_private", |
| 143 | + } |
| 144 | + ), |
| 145 | + ( |
| 146 | + "Get all repos for org gg-src-fingerprint-org except archived ones", |
| 147 | + ["--include-public-repos", "--include-forked-repos"], |
| 148 | + { |
| 149 | + "external_private", |
| 150 | + "gg_src_fingerprint_private", |
| 151 | + "gg_src_fingerprint_public", |
| 152 | + "src-fingerprint", |
| 153 | + } |
| 154 | + ), |
| 155 | + ( |
| 156 | + "Get all repos for org gg-src-fingerprint-org except forked ones", |
| 157 | + ["--include-public-repos", "--include-archived-repos"], |
| 158 | + { |
| 159 | + "external_private", |
| 160 | + "gg_src_fingerprint_private", |
| 161 | + "gg_src_fingerprint_private_archive", |
| 162 | + "gg_src_fingerprint_public", |
| 163 | + "gg_src_fingerprint_public_archive", |
| 164 | + } |
| 165 | + ), |
| 166 | + ( |
| 167 | + "Get all repos for org gg-src-fingerprint-org", |
| 168 | + ["--include-public-repos", "--include-archived-repos", "--include-forked-repos"], |
| 169 | + { |
| 170 | + "external_private", |
| 171 | + "gg_src_fingerprint_private", |
| 172 | + "gg_src_fingerprint_private_archive", |
| 173 | + "gg_src_fingerprint_public", |
| 174 | + "gg_src_fingerprint_public_archive", |
| 175 | + "src-fingerprint", |
| 176 | + } |
| 177 | + ) |
| 178 | + ] |
| 179 | +) |
| 180 | +def test_src_fingerprint_on_org(title, cmd_args, expected_output_repos): |
| 181 | + run_src_fingerprint(provider="github", args=["--object", "gg-src-fingerprint-org"]+cmd_args) |
| 182 | + output_repos = get_output_repos("fingerprints.jsonl") |
| 183 | + os.remove("fingerprints.jsonl") |
| 184 | + assert output_repos == expected_output_repos |
0 commit comments