Files
cutdex/tool/tests/package_boundaries_test.py
T
lingjhf 0343e2f723 refactor(build): unify SDK configuration and release tooling
Pin Flutter with FVM and share local environment loading across IDE, Python, and Dart entrypoints. Keep CI on the verified preinstalled SDK and skip local overrides.

Extract common build presets, process execution, and file hashing. Propagate project roots through SDK selection and release commands, and migrate macOS release, signing, and DMG orchestration to Python.

Reuse local check suites in CI, support Android source overrides, and update documentation and regression tests.
2026-10-06 23:34:15 +08:00

104 lines
5.0 KiB
Python

"""Catch dependencies that Dart's shared workspace resolution can hide."""
import importlib.util
from pathlib import Path
import tempfile
import unittest
spec = importlib.util.spec_from_file_location(
'boundaries', Path(__file__).resolve().parents[1] / 'check_package_boundaries.py')
boundaries = importlib.util.module_from_spec(spec)
spec.loader.exec_module(boundaries)
class PackageBoundaryTests(unittest.TestCase):
def setUp(self):
temp = tempfile.TemporaryDirectory()
self.addCleanup(temp.cleanup)
self.root = Path(temp.name)
for package in boundaries.RUNTIME:
self.write(f'packages/{package}/pubspec.yaml', f'''name: {package}
resolution: workspace
publish_to: none
dependencies:
flutter:
sdk: flutter
dev_dependencies:
flutter_test:
sdk: flutter
''')
def write(self, path, content):
target = self.root / path
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(content)
def test_reverse_dependency_is_rejected_even_when_unused(self):
path = self.root / 'packages/cutdex_native/pubspec.yaml'
path.write_text(path.read_text().replace('dependencies:\n',
'dependencies:\n cutdex: any\n', 1))
self.assertTrue(any('forbidden dependency cutdex' in error
for error in boundaries.check(self.root)))
def test_workspace_resolution_does_not_authorize_transitive_imports(self):
self.write('packages/cutdex_ui/lib/component.dart', "import 'package:path/path.dart';")
self.assertTrue(any('undeclared import package:path/' in error
for error in boundaries.check(self.root)))
def test_application_requires_public_package_entrypoints(self):
self.write('lib/app.dart', "import 'package:cutdex_ui/src/widgets/app_item.dart';")
self.write('test/app_test.dart', "import '../packages/cutdex_native/lib/cutdex_native.dart';")
errors = boundaries.check(self.root)
self.assertEqual(len(errors), 2, errors)
def test_integration_tests_cannot_import_sdk_implementation(self):
self.write('integration_test/save_test.dart',
"import 'package:cutdex_project/src/persistence/timeline_codec.dart';")
self.assertTrue(any('private import' in error
for error in boundaries.check(self.root)))
def test_package_tests_cannot_escape_the_package(self):
self.write('packages/cutdex_ui/test/component_test.dart',
"import '../../../lib/app.dart';")
self.assertTrue(any('escaping import' in error
for error in boundaries.check(self.root)))
def test_application_cannot_import_sibling_repository_sources(self):
self.write('lib/bridge.dart', "export '../../cutdex_sdk/lib/editor.dart';")
self.write('integration_test/save_test.dart',
"import '../../cutdex_sdk/packages/project/lib/cutdex_project.dart';")
self.assertEqual(len(boundaries.check(self.root)), 2)
def test_application_library_cannot_import_test_or_tool_sources(self):
self.write('lib/bridge.dart', "import '../test/support/fake.dart';")
self.write('lib/tool.dart', "export '../tool/testing/fixture.dart';")
self.assertEqual(len(boundaries.check(self.root)), 2)
def test_public_imports_and_own_implementation_are_allowed(self):
self.write('lib/app.dart', "import 'package:cutdex_ui/widgets.dart';")
self.write('packages/cutdex_ui/lib/widgets.dart', "export 'src/widget.dart';")
self.write('packages/cutdex_ui/test/widget_test.dart',
"import 'package:cutdex_ui/widgets.dart';\nimport 'package:flutter_test/flutter_test.dart';")
self.assertEqual(boundaries.check(self.root), [])
def test_utf8_sources_are_read_independently_of_windows_locale(self):
source = self.root / 'lib/app.dart'
source.parent.mkdir(parents=True)
source.write_text("const label = '剪辑 🎬';\n", encoding='utf-8')
self.assertEqual(boundaries.check(self.root), [])
def test_conditional_imports_and_exports_cannot_hide_private_sources(self):
self.write('lib/app.dart', "import 'stub.dart' if (dart.library.io) 'package:cutdex_ui/src/widgets/app_item.dart';")
self.write('lib/export.dart', "export 'stub.dart' if (dart.library.io) 'package:cutdex_native/src/file_drop.dart';")
self.assertEqual(len(boundaries.check(self.root)), 2)
def test_normalized_package_uris_cannot_hide_private_sources(self):
self.write('lib/app.dart', "import 'package:cutdex_ui/public/../src/widgets/app_item.dart';")
self.assertTrue(any('private import' in error for error in boundaries.check(self.root)))
def test_fixture_strings_and_comments_are_not_imports(self):
self.write('lib/app.dart', '''// import 'package:cutdex_ui/src/fake.dart';
/* export 'package:cutdex_ui/src/fake.dart'; */
const fixture = "import 'package:cutdex_ui/src/fake.dart';";
''')
self.assertEqual(boundaries.check(self.root), [])