|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | +import unittest |
| 4 | + |
| 5 | +from repokg.code import walk |
| 6 | +from repokg.deps import _js_configs, _jsonc_loads, collect |
| 7 | + |
| 8 | + |
| 9 | +def write(root, rel, text): |
| 10 | + path = os.path.join(root, rel) |
| 11 | + os.makedirs(os.path.dirname(path), exist_ok=True) |
| 12 | + with open(path, "w", encoding="utf-8") as f: |
| 13 | + f.write(text) |
| 14 | + |
| 15 | + |
| 16 | +class TestJsoncLoads(unittest.TestCase): |
| 17 | + def test_plain_json(self): |
| 18 | + self.assertEqual(_jsonc_loads('{"a": [1, 2]}'), {"a": [1, 2]}) |
| 19 | + |
| 20 | + def test_comments_stripped(self): |
| 21 | + self.assertEqual(_jsonc_loads( |
| 22 | + '{\n' |
| 23 | + ' // line comment\n' |
| 24 | + ' "a": 1, /* block\n' |
| 25 | + ' comment */ "b": 2\n' |
| 26 | + '}'), {"a": 1, "b": 2}) |
| 27 | + |
| 28 | + def test_trailing_commas_stripped(self): |
| 29 | + self.assertEqual(_jsonc_loads('{"a": [1, 2, ], "b": {"c": 3,},}'), |
| 30 | + {"a": [1, 2], "b": {"c": 3}}) |
| 31 | + |
| 32 | + def test_comment_lookalikes_inside_strings_survive(self): |
| 33 | + self.assertEqual(_jsonc_loads( |
| 34 | + '{"url": "https://x.dev", "glob": "src/**/*", "csv": "a,}"}'), |
| 35 | + {"url": "https://x.dev", "glob": "src/**/*", "csv": "a,}"}) |
| 36 | + |
| 37 | + def test_escaped_quote_inside_string(self): |
| 38 | + self.assertEqual(_jsonc_loads('{"a": "say \\"hi\\" // ok",}'), |
| 39 | + {"a": 'say "hi" // ok'}) |
| 40 | + |
| 41 | + def test_invalid_returns_none(self): |
| 42 | + self.assertIsNone(_jsonc_loads("{not json")) |
| 43 | + self.assertIsNone(_jsonc_loads("")) |
| 44 | + |
| 45 | + |
| 46 | +class TestJsConfigs(unittest.TestCase): |
| 47 | + def setUp(self): |
| 48 | + self.tmp = tempfile.TemporaryDirectory() |
| 49 | + self.repo = self.tmp.name |
| 50 | + |
| 51 | + def tearDown(self): |
| 52 | + self.tmp.cleanup() |
| 53 | + |
| 54 | + def tree(self): |
| 55 | + return dict(walk(self.repo)) |
| 56 | + |
| 57 | + def test_baseurl_and_paths(self): |
| 58 | + write(self.repo, "tsconfig.json", |
| 59 | + '{"compilerOptions": {"baseUrl": "./src",' |
| 60 | + ' "paths": {"@/*": ["./*"]}}}') |
| 61 | + write(self.repo, "src/app.ts", "") |
| 62 | + configs = _js_configs(self.repo, self.tree()) |
| 63 | + self.assertEqual(configs, {"": ("src", [("@/*", ["./*"])], "src")}) |
| 64 | + |
| 65 | + def test_paths_without_baseurl_resolve_from_config_dir(self): |
| 66 | + write(self.repo, "web/tsconfig.json", |
| 67 | + '{"compilerOptions": {"paths": {"@/*": ["./src/*"]}}}') |
| 68 | + configs = _js_configs(self.repo, self.tree()) |
| 69 | + self.assertEqual(configs["web"], ("web", [("@/*", ["./src/*"])], None)) |
| 70 | + |
| 71 | + def test_jsconfig_supported_tsconfig_preferred(self): |
| 72 | + write(self.repo, "a/jsconfig.json", |
| 73 | + '{"compilerOptions": {"baseUrl": "."}}') |
| 74 | + write(self.repo, "b/jsconfig.json", |
| 75 | + '{"compilerOptions": {"baseUrl": "./x"}}') |
| 76 | + write(self.repo, "b/tsconfig.json", |
| 77 | + '{"compilerOptions": {"baseUrl": "./y"}}') |
| 78 | + configs = _js_configs(self.repo, self.tree()) |
| 79 | + self.assertEqual(configs["a"], ("a", [], "a")) |
| 80 | + self.assertEqual(configs["b"], ("b/y", [], "b/y")) |
| 81 | + |
| 82 | + def test_config_without_aliases_ignored(self): |
| 83 | + write(self.repo, "tsconfig.json", |
| 84 | + '{"extends": "./tsconfig.base.json",' |
| 85 | + ' "compilerOptions": {"strict": true}}') |
| 86 | + self.assertEqual(_js_configs(self.repo, self.tree()), {}) |
| 87 | + |
| 88 | + def test_most_specific_pattern_first(self): |
| 89 | + write(self.repo, "tsconfig.json", |
| 90 | + '{"compilerOptions": {"paths": {' |
| 91 | + '"@/*": ["./src/*"], "@/lib/*": ["./lib/*"], "@cfg": ["./cfg.ts"]}}}') |
| 92 | + _, patterns, _ = _js_configs(self.repo, self.tree())[""] |
| 93 | + self.assertEqual([p for p, _ in patterns], ["@cfg", "@/lib/*", "@/*"]) |
| 94 | + |
| 95 | + |
| 96 | +class TestJsAliasEdges(unittest.TestCase): |
| 97 | + def setUp(self): |
| 98 | + self.tmp = tempfile.TemporaryDirectory() |
| 99 | + self.repo = self.tmp.name |
| 100 | + |
| 101 | + def tearDown(self): |
| 102 | + self.tmp.cleanup() |
| 103 | + |
| 104 | + def edges(self): |
| 105 | + return {(e["from"], e["to"]): e["count"] |
| 106 | + for e in collect(self.repo) if e["lang"] == "JS/TS"} |
| 107 | + |
| 108 | + def test_alias_import_matches_equivalent_relative_import(self): |
| 109 | + write(self.repo, "tsconfig.json", |
| 110 | + '{"compilerOptions": {"baseUrl": ".",' |
| 111 | + ' "paths": {"@/*": ["./src/*"]}}}') |
| 112 | + write(self.repo, "src/components/button.tsx", "export const B = 1\n") |
| 113 | + write(self.repo, "pages/index.tsx", |
| 114 | + "import { B } from '@/components/button'\n" |
| 115 | + "import { B as B2 } from '../src/components/button'\n") |
| 116 | + self.assertEqual(self.edges(), |
| 117 | + {("pages", "src/components"): 2}) |
| 118 | + |
| 119 | + def test_bare_third_party_imports_drop(self): |
| 120 | + write(self.repo, "tsconfig.json", |
| 121 | + '{"compilerOptions": {"paths": {"@/*": ["./src/*"]}}}') |
| 122 | + write(self.repo, "src/util.ts", "export const u = 1\n") |
| 123 | + write(self.repo, "app/main.ts", |
| 124 | + "import React from 'react'\n" |
| 125 | + "import fs from 'node:fs'\n" |
| 126 | + "const x = require('lodash/get')\n") |
| 127 | + self.assertEqual(self.edges(), {}) |
| 128 | + |
| 129 | + def test_exact_alias_to_file_resolves_to_parent_dir(self): |
| 130 | + write(self.repo, "tsconfig.json", |
| 131 | + '{"compilerOptions": {"paths": {"@config": ["./src/config.ts"]}}}') |
| 132 | + write(self.repo, "src/config.ts", "export const c = 1\n") |
| 133 | + write(self.repo, "app/main.ts", "import { c } from '@config'\n") |
| 134 | + self.assertEqual(self.edges(), {("app", "src"): 1}) |
| 135 | + |
| 136 | + def test_baseurl_bare_import(self): |
| 137 | + write(self.repo, "jsconfig.json", |
| 138 | + '{"compilerOptions": {"baseUrl": "."}}') |
| 139 | + write(self.repo, "lib/util.js", "module.exports = {}\n") |
| 140 | + write(self.repo, "app/main.js", "const u = require('lib/util')\n") |
| 141 | + self.assertEqual(self.edges(), {("app", "lib"): 1}) |
| 142 | + |
| 143 | + def test_nearest_config_wins_and_shadows_root(self): |
| 144 | + write(self.repo, "tsconfig.json", |
| 145 | + '{"compilerOptions": {"paths": {"~/*": ["./shared/*"]}}}') |
| 146 | + write(self.repo, "shared/log.ts", "export const l = 1\n") |
| 147 | + write(self.repo, "packages/app/tsconfig.json", |
| 148 | + '{"compilerOptions": {"paths": {"@/*": ["./src/*"]}}}') |
| 149 | + write(self.repo, "packages/app/src/db.ts", "export const d = 1\n") |
| 150 | + write(self.repo, "packages/app/main.ts", |
| 151 | + "import { d } from '@/db'\n" # nearest config's alias |
| 152 | + "import { l } from '~/log'\n") # root alias: shadowed, drops |
| 153 | + write(self.repo, "tools/gen.ts", |
| 154 | + "import { l } from '~/log'\n") # root config applies here |
| 155 | + self.assertEqual(self.edges(), { |
| 156 | + ("packages/app", "packages/app/src"): 1, |
| 157 | + ("tools", "shared"): 1, |
| 158 | + }) |
| 159 | + |
| 160 | + def test_unresolved_alias_target_drops(self): |
| 161 | + write(self.repo, "tsconfig.json", |
| 162 | + '{"compilerOptions": {"paths": {"@/*": ["./src/*"]}}}') |
| 163 | + write(self.repo, "src/a.ts", "export const a = 1\n") |
| 164 | + write(self.repo, "app/main.ts", "import { g } from '@/gone/deep/x'\n") |
| 165 | + self.assertEqual(self.edges(), {}) |
| 166 | + |
| 167 | + def test_first_existing_paths_value_wins(self): |
| 168 | + write(self.repo, "tsconfig.json", |
| 169 | + '{"compilerOptions": {"paths": {"@/*": ["./missing/*", "./src/*"]}}}') |
| 170 | + write(self.repo, "src/a.ts", "export const a = 1\n") |
| 171 | + write(self.repo, "app/main.ts", "import { a } from '@/a'\n") |
| 172 | + self.assertEqual(self.edges(), {("app", "src"): 1}) |
| 173 | + |
| 174 | + def test_relative_imports_unchanged_without_configs(self): |
| 175 | + write(self.repo, "src/a.ts", "export const a = 1\n") |
| 176 | + write(self.repo, "app/main.ts", |
| 177 | + "import { a } from '../src/a'\n" |
| 178 | + "import missing from '@/nope'\n") |
| 179 | + self.assertEqual(self.edges(), {("app", "src"): 1}) |
| 180 | + |
| 181 | + def test_jsonc_config_with_comments_and_trailing_commas(self): |
| 182 | + write(self.repo, "tsconfig.json", |
| 183 | + '{\n' |
| 184 | + ' // aliases\n' |
| 185 | + ' "compilerOptions": {\n' |
| 186 | + ' "baseUrl": ".", /* root */\n' |
| 187 | + ' "paths": {\n' |
| 188 | + ' "@/*": ["./src/*"],\n' |
| 189 | + ' },\n' |
| 190 | + ' },\n' |
| 191 | + '}\n') |
| 192 | + write(self.repo, "src/a.ts", "export const a = 1\n") |
| 193 | + write(self.repo, "app/main.ts", "import { a } from '@/a'\n") |
| 194 | + self.assertEqual(self.edges(), {("app", "src"): 1}) |
| 195 | + |
| 196 | + |
| 197 | +if __name__ == "__main__": |
| 198 | + unittest.main() |
0 commit comments