flutter/packages/flutter_tools/test/general.shard/compute_dev_dependencies_test.dart
Andrew Kolos a9c50335c7
remove dependency on Usage from Pub class (#162279)
Towards https://github.com/flutter/flutter/issues/150575

Removes more usage of `Usage`. This PR is scoped to removing all
references to it in `pub`-related code.

<details>

<summary> Pre-launch checklist </summary> 


- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

</details>


If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
2025-01-28 17:32:27 +00:00

423 lines
9.8 KiB
Dart

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/compute_dev_dependencies.dart';
import 'package:flutter_tools/src/dart/pub.dart';
import 'package:flutter_tools/src/project.dart';
import '../src/common.dart';
import '../src/fake_process_manager.dart';
import '../src/fakes.dart';
const String _dartBin = 'bin/cache/dart-sdk/bin/dart';
// For all of these examples, imagine the following package structure:
//
// /
// /my_app
// pubspec.yaml
// /package_a
// pubspec.yaml
// /package_b
// pubspec.yaml
// /package_c
// pubspec.yaml
void main() {
late FileSystem fileSystem;
late FlutterProject project;
late BufferLogger logger;
setUp(() {
Cache.flutterRoot = '';
fileSystem = MemoryFileSystem.test();
project = FlutterProject.fromDirectoryTest(fileSystem.currentDirectory);
logger = BufferLogger.test();
});
Pub pub(ProcessManager processManager) {
return Pub.test(
fileSystem: fileSystem,
logger: logger,
processManager: processManager,
platform: FakePlatform(),
botDetector: const FakeBotDetector(false),
stdio: FakeStdio(),
);
}
test('no dev dependencies at all', () async {
// Simulates the following:
//
// # /my_app/pubspec.yaml
// name: my_app
// dependencies:
// package_a:
//
// # /package_a/pubspec.yaml
// name: package_a
// dependencies:
// package_b:
final ProcessManager processes = _dartPubDepsReturns('''
{
"root": "my_app",
"packages": [
{
"name": "my_app",
"kind": "root",
"dependencies": [
"package_a",
"package_b"
],
"directDependencies": [
"package_a"
],
"devDependencies": []
},
{
"name": "package_a",
"kind": "direct",
"dependencies": [
"package_b"
],
"directDependencies": [
"package_b"
]
},
{
"name": "package_b",
"kind": "transitive",
"dependencies": [],
"directDependencies": []
}
]
}''', project: project);
final Set<String> dependencies = await computeExclusiveDevDependencies(
pub(processes),
project: project,
logger: logger,
);
expect(dependencies, isEmpty, reason: 'There are no dev_dependencies of "my_app".');
});
test('dev dependency', () async {
// Simulates the following:
//
// # /my_app/pubspec.yaml
// name: my_app
// dependencies:
// package_a:
//
// dev_dependencies:
// package_b:
//
// # /package_a/pubspec.yaml
// name: package_a
final ProcessManager processes = _dartPubDepsReturns('''
{
"root": "my_app",
"packages": [
{
"name": "my_app",
"kind": "root",
"dependencies": [
"package_a",
"package_b"
],
"directDependencies": [
"package_a"
],
"devDependencies": [
"package_b"
]
},
{
"name": "package_a",
"kind": "direct",
"dependencies": [],
"directDependencies": []
},
{
"name": "package_b",
"kind": "dev",
"dependencies": [],
"directDependencies": []
}
]
}''', project: project);
final Set<String> dependencies = await computeExclusiveDevDependencies(
pub(processes),
project: project,
logger: logger,
);
expect(dependencies, <String>{
'package_b',
}, reason: 'There is a single dev_dependency of my_app: package_b.');
});
test('dev used as a non-dev dependency transitively', () async {
// Simulates the following:
//
// # /my_app/pubspec.yaml
// name: my_app
// dependencies:
// package_a:
//
// dev_dependencies:
// package_b:
//
// # /package_a/pubspec.yaml
// name: package_a
// dependencies:
// package_b:
final ProcessManager processes = _dartPubDepsReturns('''
{
"root": "my_app",
"packages": [
{
"name": "my_app",
"kind": "root",
"dependencies": [
"package_a",
"package_b"
],
"directDependencies": [
"package_a"
],
"devDependencies": [
"package_b"
]
},
{
"name": "package_a",
"kind": "direct",
"dependencies": [
"package_b"
],
"directDependencies": [
"package_b"
]
},
{
"name": "package_b",
"kind": "dev",
"dependencies": [],
"directDependencies": []
}
]
}''', project: project);
final Set<String> dependencies = await computeExclusiveDevDependencies(
pub(processes),
project: project,
logger: logger,
);
expect(
dependencies,
isEmpty,
reason: 'There is a dev_dependency also used as a standard dependency',
);
});
test('combination of an included and excluded dev_dependency', () async {
// Simulates the following:
//
// # /my_app/pubspec.yaml
// name: my_app
// dependencies:
// package_a:
//
// dev_dependencies:
// package_b:
// package_c:
//
// # /package_a/pubspec.yaml
// name: package_a
// dependencies:
// package_b:
//
// # /package_b/pubspec.yaml
// name: package_b
//
// # /package_c/pubspec.yaml
// name: package_c
final ProcessManager processes = _dartPubDepsReturns('''
{
"root": "my_app",
"packages": [
{
"name": "my_app",
"kind": "root",
"dependencies": [
"package_a",
"package_b"
],
"directDependencies": [
"package_a"
],
"devDependencies": [
"package_b",
"package_c"
]
},
{
"name": "package_a",
"kind": "direct",
"dependencies": [
"package_b"
],
"directDependencies": [
"package_b"
]
},
{
"name": "package_b",
"kind": "dev",
"dependencies": [
"package_c"
],
"directDependencies": [
"package_c"
]
},
{
"name": "package_c",
"kind": "dev",
"dependencies": [],
"directDependencies": []
}
]
}''', project: project);
final Set<String> dependencies = await computeExclusiveDevDependencies(
pub(processes),
project: project,
logger: logger,
);
expect(dependencies, <String>{
'package_c',
}, reason: 'package_b is excluded but package_c should not');
});
test('omitted devDependencies in app package', () async {
// Simulates the following:
//
// # /my_app/pubspec.yaml
// name: my_app
// dependencies:
// package_a:
//
// # /package_a/pubspec.yaml
// name: package_a
final ProcessManager processes = _dartPubDepsReturns('''
{
"root": "my_app",
"packages": [
{
"name": "my_app",
"kind": "root",
"dependencies": [
"package_a"
],
"directDependencies": [
"package_a"
]
},
{
"name": "package_a",
"kind": "direct",
"dependencies": [],
"directDependencies": []
}
]
}''', project: project);
final Set<String> dependencies = await computeExclusiveDevDependencies(
pub(processes),
project: project,
logger: logger,
);
expect(
dependencies,
isEmpty,
reason: 'No devDependencies: [] specified but still parsed successfully',
);
});
test('throws and logs on invalid JSON', () async {
final ProcessManager processes = _dartPubDepsReturns('''
{
"root": "my_app",
"packages": [
{
"name": "my_app",
"kind": "root",
"dependencies": [
"package_a",
"package_b"
],
"directDependencies": [
1
],
"devDependencies": []
},
{
"name": "package_a",
"kind": "direct",
"dependencies": [
"package_b"
],
"directDependencies": [
"package_b"
]
},
{
"name": "package_b",
"kind": "transitive",
"dependencies": [],
"directDependencies": []
}
]
}''', project: project);
await expectLater(
computeExclusiveDevDependencies(pub(processes), project: project, logger: logger),
throwsA(
isA<StateError>().having(
(StateError e) => e.message,
'message',
contains('dart pub deps --json had unexpected output'),
),
),
);
expect(
logger.traceText,
contains('"root": "my_app"'),
reason: 'Stdout should include the JSON blob',
);
});
}
ProcessManager _dartPubDepsReturns(String dartPubDepsOutput, {required FlutterProject project}) {
return FakeProcessManager.list(<FakeCommand>[
FakeCommand(
command: const <String>[_dartBin, 'pub', '--suppress-analytics', 'deps', '--json'],
stdout: dartPubDepsOutput,
workingDirectory: project.directory.path,
),
]);
}