Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove dependency overrides from pubspec_overrides.yaml in melos clean #290

Merged
merged 2 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions packages/melos/lib/src/commands/clean.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mixin _CleanMixin on _Melos {
/// Cleans the workspace of all files generated by Melos.
cleanWorkspace(workspace);

workspace.filteredPackages.values.forEach(_cleanPackage);
await Future.wait(workspace.filteredPackages.values.map(_cleanPackage));

await cleanIntelliJ(workspace);

Expand All @@ -31,7 +31,7 @@ mixin _CleanMixin on _Melos {
}
}

void _cleanPackage(Package package) {
Future<void> _cleanPackage(Package package) async {
final pathsToClean = [
...cleanablePubFilePaths,
'.dart_tool',
Expand All @@ -40,7 +40,19 @@ mixin _CleanMixin on _Melos {
for (final generatedPubFilePath in pathsToClean) {
final file = File(join(package.path, generatedPubFilePath));
if (file.existsSync()) {
file.deleteSync(recursive: true);
await file.delete(recursive: true);
}
}

// Remove any Melos generated dependency overrides from
// `pubspec_overrides.yaml`.
final pubspecOverridesFile =
File(join(package.path, 'pubspec_overrides.yaml'));
if (pubspecOverridesFile.existsSync()) {
final contents = await pubspecOverridesFile.readAsString();
final updatedContents = mergeMelosPubspecOverrides({}, contents);
if (updatedContents != null) {
await pubspecOverridesFile.writeAsString(updatedContents);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure but do we want to delete the file here in an else since it's empty and since it was most likely created by Melos?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mergeMelosPubspecOverrides allows overrides that don't come from melos, so passing in {} does not guarantee that pubspec_overrides.yaml is empty. But we can check for that and if there are no other overrides, then I think it's a good idea to delete the file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
}
Expand Down
65 changes: 65 additions & 0 deletions packages/melos/test/commands/clean_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import 'package:glob/glob.dart';
import 'package:melos/melos.dart';
import 'package:melos/src/common/utils.dart';
import 'package:path/path.dart' as p;
import 'package:pub_semver/pub_semver.dart';
import 'package:pubspec/pubspec.dart';
import 'package:test/test.dart';

import '../matchers.dart';
import '../utils.dart';

void main() {
group('clean', () {
test(
'removes dependency overrides from pubspec_overrides.yaml',
() async {
final workspaceDir = createTemporaryWorkspaceDirectory(
configBuilder: (path) => MelosWorkspaceConfig(
path: path,
name: 'test_workspace',
packages: [Glob('packages/**')],
commands: const CommandConfigs(
bootstrap: BootstrapCommandConfigs(
usePubspecOverrides: true,
),
),
),
);

await createProject(workspaceDir, const PubSpec(name: 'a'));
final packageBDir = await createProject(
workspaceDir,
PubSpec(
name: 'b',
dependencies: {'a': HostedReference(VersionConstraint.any)},
),
);
final pubspecOverrides =
p.join(packageBDir.path, 'pubspec_overrides.yaml');

final config = await MelosWorkspaceConfig.fromDirectory(workspaceDir);
final logger = TestLogger();
final melos = Melos(config: config, logger: logger);
await melos.bootstrap();

expect(
pubspecOverrides,
yamlFile({
'dependency_overrides': {
'a': {'path': '../a'}
}
}),
);

await melos.clean();

expect(
pubspecOverrides,
yamlFile({'dependency_overrides': null}),
);
},
skip: !isPubspecOverridesSupported(),
);
});
}
163 changes: 163 additions & 0 deletions packages/melos/test/matchers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@
* limitations under the License.
*/

import 'dart:io' as io;

import 'package:file/file.dart';
import 'package:melos/src/common/validation.dart';
import 'package:melos/src/package.dart';
import 'package:path/path.dart' as p;
import 'package:test/test.dart';
import 'package:yaml/yaml.dart';

Matcher packageNamed(dynamic matcher) => _PackageNameMatcher(matcher);

Expand Down Expand Up @@ -83,3 +88,161 @@ TypeMatcher<MelosConfigException> isMelosConfigException({
Matcher throwsMelosConfigException({Object? message}) {
return throwsA(isMelosConfigException(message: message));
}

Matcher fileContents(Object? matcher) => _FileContents(wrapMatcher(matcher));

class _FileContents extends Matcher {
_FileContents(this._matcher);

final Matcher _matcher;

@override
bool matches(Object? item, Map matchState) {
final io.File file;
if (item is File) {
file = item;
} else if (item is String) {
file = io.File(p.normalize(item));
} else {
matchState['fileContents.invalidItem'] = true;
return false;
}

if (!file.existsSync()) {
matchState['fileContents.fileMissing'] = true;
return false;
}

final contents = file.readAsStringSync();
if (_matcher.matches(contents, matchState)) {
return true;
}
addStateInfo(
matchState,
<String, String>{'fileContents.contents': contents},
);
return false;
}

@override
Description describe(Description description) => description
..add('file with contents that ')
..addDescriptionOf(_matcher);

@override
Description describeMismatch(
Object? item,
Description mismatchDescription,
Map matchState,
bool verbose,
) {
if (matchState['fileContents.invalidItem'] == true) {
return mismatchDescription.add('is not a reference to a file');
}

if (matchState['fileContents.fileMissing'] == true) {
return mismatchDescription.add('does not exist');
}

final contents = matchState['fileContents.contents'] as String;
mismatchDescription
..add('contains ')
..addDescriptionOf(contents);

final innerDescription = StringDescription();
_matcher.describeMismatch(
contents,
innerDescription,
matchState['state'] as Map,
verbose,
);
if (innerDescription.length > 0) {
mismatchDescription.add(' which ').add(innerDescription.toString());
}

return mismatchDescription;
}
}

Matcher yaml(Object? matcher) => _Yaml(wrapMatcher(matcher));

class _Yaml extends Matcher {
_Yaml(this._matcher);

final Matcher _matcher;

@override
bool matches(Object? item, Map matchState) {
final String yamlString;
if (item is String) {
yamlString = item;
} else {
matchState['yaml.invalidItem'] = true;
return false;
}

Object? value;
try {
value = loadYaml(yamlString);
} catch (e, s) {
matchState['yaml.error'] = e;
matchState['yaml.stack'] = s;
return false;
}

if (_matcher.matches(value, matchState)) {
return true;
}
addStateInfo(
matchState,
<String, Object?>{'yaml.value': value},
);
return false;
}

@override
Description describe(Description description) => description
..add('is valid Yaml string with parsed value that ')
..addDescriptionOf(_matcher);

@override
Description describeMismatch(
Object? item,
Description mismatchDescription,
Map matchState,
bool verbose,
) {
if (matchState['yaml.invalidItem'] == true) {
return mismatchDescription.add(' must be a String');
}

final error = matchState['yaml.error'] as Object?;
final stack = matchState['yaml.stack'] as StackTrace?;
if (error != null) {
return mismatchDescription.add('could not be parsed: \n')
..addDescriptionOf(error)
..add('\n')
..add(stack.toString());
}

final value = matchState['yaml.value'] as Object?;
mismatchDescription
..add('has the parsed value ')
..addDescriptionOf(value);

final innerDescription = StringDescription();
_matcher.describeMismatch(
value,
innerDescription,
matchState['state'] as Map,
verbose,
);
if (innerDescription.length > 0) {
mismatchDescription.add(' which ').add(innerDescription.toString());
}

return mismatchDescription;
}
}

Matcher yamlFile(Object? matcher) => fileContents(yaml(matcher));