Download stackgo/tests/test_repoinfo.py from s0duku/the-stack-go: direct link, hf CLI and curl.
- Browser
- Download file 15.6 kB
-
https://huggingface.co/datasets/s0duku/the-stack-go/resolve/main/stackgo/tests/test_repoinfo.py
- Command line
-
hf download hf://datasets/s0duku/the-stack-go/stackgo/tests/test_repoinfo.py
-
curl -L -o test_repoinfo.py https://huggingface.co/datasets/s0duku/the-stack-go/resolve/main/stackgo/tests/test_repoinfo.py
15.6 kB
| from stackbuild import Repo, RepoFile, concatenation_dfs, dependency_context, repository_windows, scan | |
| from stackbuild.repo_extract import language_name | |
| def test_dependency_order_and_metadata(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("src/main.py", "from src.config import VALUE\nprint(VALUE)\n", "python"), | |
| RepoFile("src/config.py", "VALUE = 1\n", "python"), | |
| ]) | |
| info = scan(repo, use_treesitter=False) | |
| assert info.topological_order.index("src/config.py") < info.topological_order.index("src/main.py") | |
| assert info.stats["resolved_edges"] == 1 | |
| assert info.edges[0].source == "src/main.py" | |
| def test_cycles_are_retained(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("a.py", "import b\n", "python"), | |
| RepoFile("b.py", "import a\n", "python"), | |
| ]) | |
| info = scan(repo, use_treesitter=False) | |
| assert any(set(component) == {"a.py", "b.py"} for component in info.strongly_connected_components) | |
| def test_cycle_component_preserves_external_dependency_order(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("base.py", "VALUE = 1\n", "python"), | |
| RepoFile("a.py", "import b\nimport base\n", "python"), | |
| RepoFile("b.py", "import a\n", "python"), | |
| RepoFile("consumer.py", "import b\n", "python"), | |
| ]) | |
| info = scan(repo, use_treesitter=False) | |
| positions = {path: index for index, path in enumerate(info.topological_order)} | |
| assert positions["base.py"] < positions["a.py"] | |
| assert positions["base.py"] < positions["b.py"] | |
| assert positions["a.py"] < positions["consumer.py"] | |
| assert positions["b.py"] < positions["consumer.py"] | |
| assert concatenation_dfs(info) == info.topological_order | |
| assert info.concatenation_tree is not None | |
| assert info.concatenation_tree.kind == "repo" | |
| cycle_nodes = [node for node in info.concatenation_tree.children if node.kind == "cycle"] | |
| assert len(cycle_nodes) == 1 | |
| assert {child.path for child in cycle_nodes[0].children} == {"a.py", "b.py"} | |
| def test_concatenation_tree_covers_every_file_once_with_metadata(): | |
| repo = Repo("owner/demo", "rev1", [ | |
| RepoFile("README.md", "docs\n", "Markdown"), | |
| RepoFile("main.py", "import service\n", "python"), | |
| RepoFile("service.py", "VALUE = 1\n", "python"), | |
| ]) | |
| info = scan(repo) | |
| assert info.concatenation_tree is not None | |
| assert info.concatenation_tree.name == "owner/demo" | |
| assert info.concatenation_tree.bytes == sum(file.bytes for file in info.files) | |
| assert concatenation_dfs(info) == info.topological_order | |
| assert set(concatenation_dfs(info)) == {"README.md", "main.py", "service.py"} | |
| main = next(node for node in info.concatenation_tree.children if node.path == "main.py") | |
| assert main.depends_on == ("service.py",) | |
| def test_tree_sitter_backend_is_reported_when_grammar_is_cached(): | |
| repo = Repo("demo", "rev1", [RepoFile("main.py", "import helper\n", "python"), | |
| RepoFile("helper.py", "VALUE = 1\n", "python")]) | |
| info = scan(repo) | |
| assert info.stats["tree_sitter_files"] == 2 | |
| assert info.stats["regex_fallback_files"] == 0 | |
| def test_python_src_layout_and_external_package(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("pyproject.toml", '[tool.setuptools.packages.find]\nwhere=["src"]\n', "TOML"), | |
| RepoFile("src/pkg/main.py", "from pkg import util\nimport requests\n", "python"), | |
| RepoFile("src/pkg/util.py", "VALUE = 1\n", "python"), | |
| ]) | |
| info = scan(repo) | |
| assert [(edge.source, edge.target) for edge in info.edges] == [ | |
| ("src/pkg/main.py", "src/pkg/util.py") | |
| ] | |
| assert info.stats["external_references"] == 1 | |
| def test_python_external_name_does_not_match_nested_module(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("app.py", "import math\nimport torch\n", "python"), | |
| RepoFile("pkg/util/math.py", "VALUE = 1\n", "python"), | |
| RepoFile("pkg/dl/torch/__init__.py", "VALUE = 1\n", "python"), | |
| ]) | |
| info = scan(repo) | |
| assert not info.edges | |
| assert info.stats["external_references"] == 2 | |
| def test_python_script_directory_import(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("examples/run.py", "from definitions import VALUE\n", "python"), | |
| RepoFile("examples/definitions.py", "VALUE = 1\n", "python"), | |
| ]) | |
| info = scan(repo) | |
| assert [(edge.source, edge.target) for edge in info.edges] == [ | |
| ("examples/run.py", "examples/definitions.py") | |
| ] | |
| def test_python_relative_imported_symbol_resolves_module(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("pkg/main.py", "from .model import Thing\n", "python"), | |
| RepoFile("pkg/model.py", "class Thing: pass\n", "python"), | |
| ]) | |
| info = scan(repo) | |
| assert [(edge.source, edge.target) for edge in info.edges] == [ | |
| ("pkg/main.py", "pkg/model.py") | |
| ] | |
| def test_python_from_current_package_import_resolves_each_module(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("pkg/__init__.py", "from . import model\n", "python"), | |
| RepoFile("pkg/model.py", "class Thing: pass\n", "python"), | |
| ]) | |
| info = scan(repo) | |
| assert [(edge.source, edge.target) for edge in info.edges] == [ | |
| ("pkg/__init__.py", "pkg/model.py") | |
| ] | |
| def test_typescript_alias_and_relative_index(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("tsconfig.json", '{"compilerOptions":{"baseUrl":".","paths":{"@/*":["src/*"]}}}', "JSON"), | |
| RepoFile("src/main.ts", "import {x} from '@/lib/x'; import y from './feature';", "TypeScript"), | |
| RepoFile("src/lib/x.ts", "export const x = 1;", "TypeScript"), | |
| RepoFile("src/feature/index.ts", "export default 1;", "TypeScript"), | |
| ]) | |
| info = scan(repo) | |
| assert {edge.target for edge in info.edges} == {"src/lib/x.ts", "src/feature/index.ts"} | |
| def test_typescript_jsonc_config_alias(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("tsconfig.json", '{// comment\n"compilerOptions":{"paths":{"@/*":["src/*"],},},}', "JSON"), | |
| RepoFile("src/main.ts", "import {x} from '@/lib/x';", "TypeScript"), | |
| RepoFile("src/lib/x.ts", "export const x = 1;", "TypeScript"), | |
| ]) | |
| info = scan(repo) | |
| assert [(edge.source, edge.target) for edge in info.edges] == [ | |
| ("src/main.ts", "src/lib/x.ts") | |
| ] | |
| def test_javascript_relative_vue_component(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("resources/router.js", "import Category from './views/Category'\n", "JavaScript"), | |
| RepoFile("resources/views/Category.vue", "<template><div/></template>\n", "Vue"), | |
| ]) | |
| info = scan(repo) | |
| assert [(edge.source, edge.target) for edge in info.edges] == [ | |
| ("resources/router.js", "resources/views/Category.vue") | |
| ] | |
| def test_rust_crate_self_and_super_module_resolution(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("Cargo.toml", '[package]\nname="demo-crate"\nversion="0.1.0"\n', "TOML"), | |
| RepoFile("src/lib.rs", "mod render; mod model;\n", "Rust"), | |
| RepoFile("src/model.rs", "pub struct Model;\n", "Rust"), | |
| RepoFile("src/render.rs", "pub mod html; pub mod template; pub trait Render {}\n", "Rust"), | |
| RepoFile("src/render/html.rs", "use super::Render; use super::template; use crate::model::Model;\n", "Rust"), | |
| RepoFile("src/render/template.rs", "pub const X: u8 = 1;\n", "Rust"), | |
| ]) | |
| info = scan(repo) | |
| assert {edge.target for edge in info.edges if edge.source == "src/render/html.rs"} == { | |
| "src/render.rs", "src/render/template.rs", "src/model.rs" | |
| } | |
| def test_rust_package_name_and_bare_mod_resolution(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("Cargo.toml", '[package]\nname="demo"\nversion="0.1.0"\n', "TOML"), | |
| RepoFile("src/lib.rs", "use std::fmt;\npub mod feature;\n", "Rust"), | |
| RepoFile("src/feature.rs", "pub struct Value;\n", "Rust"), | |
| RepoFile("tests/integration.rs", "use demo::feature::Value;\n", "Rust"), | |
| ]) | |
| info = scan(repo) | |
| assert ("src/lib.rs", "src/feature.rs") in { | |
| (edge.source, edge.target) for edge in info.edges | |
| } | |
| assert ("tests/integration.rs", "src/feature.rs") in { | |
| (edge.source, edge.target) for edge in info.edges | |
| } | |
| def test_rust_mod_rs_resolves_sibling_module_outside_src(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("Cargo.toml", '[package]\nname="demo"\nversion="0.1.0"\n', "TOML"), | |
| RepoFile("tests/read/mod.rs", "mod coff;\n", "Rust"), | |
| RepoFile("tests/read/coff.rs", "pub fn read() {}\n", "Rust"), | |
| ]) | |
| info = scan(repo) | |
| assert [(edge.source, edge.target) for edge in info.edges] == [ | |
| ("tests/read/mod.rs", "tests/read/coff.rs") | |
| ] | |
| def test_rust_workspace_crate_dependency(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("Cargo.toml", '[package]\nname="core-lib"\nversion="0.1.0"\n', "TOML"), | |
| RepoFile("src/lib.rs", "pub mod model;\n", "Rust"), | |
| RepoFile("src/model.rs", "pub struct Model;\n", "Rust"), | |
| RepoFile("crates/app/Cargo.toml", '[package]\nname="app"\nversion="0.1.0"\n' | |
| '[dependencies]\ncore-lib={path="../.."}\n', "TOML"), | |
| RepoFile("crates/app/src/main.rs", "use core_lib::model::Model;\n", "Rust"), | |
| ]) | |
| info = scan(repo) | |
| assert ("crates/app/src/main.rs", "src/model.rs") in { | |
| (edge.source, edge.target) for edge in info.edges | |
| } | |
| def test_rust_grouped_crate_root_module_reference(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("Cargo.toml", '[package]\nname="demo"\nversion="0.1.0"\n', "TOML"), | |
| RepoFile("src/lib.rs", "pub mod format;\n", "Rust"), | |
| RepoFile("src/format.rs", "pub const MAGIC: u8 = 1;\n", "Rust"), | |
| RepoFile("src/read.rs", "use crate::{format}; use format::MAGIC;\n", "Rust"), | |
| ]) | |
| info = scan(repo) | |
| assert ("src/read.rs", "src/format.rs") in { | |
| (edge.source, edge.target) for edge in info.edges | |
| } | |
| def test_rust_standard_crate_does_not_match_same_named_module(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("Cargo.toml", '[package]\nname="demo"\nversion="0.1.0"\n', "TOML"), | |
| RepoFile("src/lib.rs", "mod std; use std::io::Result;\n", "Rust"), | |
| RepoFile("src/std.rs", "pub struct Local;\n", "Rust"), | |
| ]) | |
| info = scan(repo) | |
| assert [(edge.source, edge.target) for edge in info.edges] == [ | |
| ("src/lib.rs", "src/std.rs") | |
| ] | |
| def test_deep_dependency_graph_does_not_use_python_recursion(): | |
| files = [RepoFile(f"m{i}.py", f"import m{i - 1}\n" if i else "", "python") | |
| for i in range(1500)] | |
| info = scan(Repo("deep", "rev1", files), use_treesitter=False) | |
| assert len(info.topological_order) == 1500 | |
| assert info.topological_order.index("m0.py") < info.topological_order.index("m1499.py") | |
| def test_elixir_modules_are_resolved_by_declaration(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("lib/demo/socket.ex", "defmodule Demo.Socket do\nend\n", "Elixir"), | |
| RepoFile("lib/demo/channel.ex", "defmodule Demo.Channel do\n alias Demo.Socket\nend\n", "Elixir"), | |
| ]) | |
| info = scan(repo) | |
| assert [(edge.source, edge.target) for edge in info.edges] == [ | |
| ("lib/demo/channel.ex", "lib/demo/socket.ex") | |
| ] | |
| def test_stack_language_names_are_normalized(): | |
| assert language_name("C++") == "cpp" | |
| assert language_name("C#") == "csharp" | |
| assert language_name("Shell") == "bash" | |
| def test_java_conventional_source_root_and_symbol_import(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("src/main/java/com/acme/App.java", "package com.acme; import com.acme.util.Tools.VALUE;", "Java"), | |
| RepoFile("src/main/java/com/acme/util/Tools.java", "package com.acme.util; class Tools {}", "Java"), | |
| ]) | |
| info = scan(repo) | |
| assert [(edge.source, edge.target) for edge in info.edges] == [ | |
| ("src/main/java/com/acme/App.java", "src/main/java/com/acme/util/Tools.java") | |
| ] | |
| def test_java_wildcard_import_orders_package_files(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("src/test/java/com/acme/App.java", "import com.acme.util.*; class App {}", "Java"), | |
| RepoFile("src/main/java/com/acme/util/One.java", "package com.acme.util; class One {}", "Java"), | |
| RepoFile("src/main/java/com/acme/util/Two.java", "package com.acme.util; class Two {}", "Java"), | |
| ]) | |
| info = scan(repo) | |
| assert {edge.target for edge in info.edges} == { | |
| "src/main/java/com/acme/util/One.java", "src/main/java/com/acme/util/Two.java" | |
| } | |
| assert all(info.topological_order.index(edge.target) < info.topological_order.index(edge.source) | |
| for edge in info.edges) | |
| def test_c_include_with_extension_resolves_header(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("src/main.c", '#include "helper.h"\n', "C"), | |
| RepoFile("src/helper.h", "int helper(void);\n", "C"), | |
| ]) | |
| info = scan(repo) | |
| assert [(edge.source, edge.target) for edge in info.edges] == [ | |
| ("src/main.c", "src/helper.h") | |
| ] | |
| def test_go_import_orders_all_package_files(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("go.mod", "module example.com/demo\n", "Go Module"), | |
| RepoFile("main.go", 'package main\nimport "example.com/demo/pkg"\n', "Go"), | |
| RepoFile("pkg/a.go", "package pkg\n", "Go"), | |
| RepoFile("pkg/b.go", "package pkg\n", "Go"), | |
| ]) | |
| info = scan(repo) | |
| assert {edge.target for edge in info.edges} == {"pkg/a.go", "pkg/b.go"} | |
| def test_go_external_package_does_not_match_repository_directory(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("go.mod", "module example.com/demo\n", "Go Module"), | |
| RepoFile("main.go", 'package main\nimport "context"\n', "Go"), | |
| RepoFile("context/context.go", "package context\n", "Go"), | |
| ]) | |
| info = scan(repo) | |
| assert not info.edges | |
| assert info.stats["external_references"] == 1 | |
| def test_dependency_context_and_repository_windows(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("main.py", "import service\n", "python"), | |
| RepoFile("service.py", "import model\n", "python"), | |
| RepoFile("model.py", "VALUE = 1\n", "python"), | |
| ]) | |
| info = scan(repo) | |
| assert dependency_context(info, "main.py") == ("model.py", "service.py", "main.py") | |
| assert repository_windows(info, max_bytes=30) == (("model.py", "service.py"), ("main.py",)) | |
| def test_csharp_using_resolves_declared_repository_namespace(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("src/Model/Thing.cs", "namespace Demo.Model { public class Thing {} }", "C#"), | |
| RepoFile("src/App.cs", "using Demo.Model;\nusing System;\nnamespace Demo { class App {} }", "C#"), | |
| ]) | |
| info = scan(repo) | |
| assert [(edge.source, edge.target) for edge in info.edges] == [ | |
| ("src/App.cs", "src/Model/Thing.cs") | |
| ] | |
| assert info.stats["external_references"] == 1 | |
| def test_csharp_ambiguous_namespace_is_not_expanded_to_many_hard_edges(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("src/One.cs", "namespace Demo.Model { class One {} }", "C#"), | |
| RepoFile("src/Two.cs", "namespace Demo.Model { class Two {} }", "C#"), | |
| RepoFile("src/App.cs", "using Demo.Model;\nnamespace Demo { class App {} }", "C#"), | |
| ]) | |
| info = scan(repo) | |
| assert not info.edges | |
| assert info.stats["ambiguous_references"] == 1 | |
| def test_swift_imports_are_extracted(): | |
| repo = Repo("demo", "rev1", [ | |
| RepoFile("Sources/App.swift", "import Foundation\nimport LocalKit\n", "Swift"), | |
| ]) | |
| info = scan(repo) | |
| assert {reference.name for reference in info.references} == {"Foundation", "LocalKit"} | |
| assert info.stats["external_references"] == 1 | |
| assert info.stats["unresolved_references"] == 1 | |