Skip to content

Commit

Permalink
fix: Only enforce lockfile when it exists (#704)
Browse files Browse the repository at this point in the history
* fix: Only enforce lockfile when it exists

* Add test for enforced pubspec lock without lockfile
  • Loading branch information
spydon authored Apr 24, 2024
1 parent 5e588ef commit be94ada
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/melos/lib/src/commands/bootstrap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ mixin _BootstrapMixin on _CleanMixin {
CommandWithLifecycle.bootstrap,
() async {
final bootstrapCommandConfig = workspace.config.commands.bootstrap;
late final hasLockFile =
File(p.join(workspace.path, 'pubspec.lock')).existsSync();
final enforceLockfileConfigValue =
workspace.config.commands.bootstrap.enforceLockfile;
final shouldEnforceLockfile =
bootstrapCommandConfig.enforceLockfile || enforceLockfile;
(enforceLockfileConfigValue || enforceLockfile) && hasLockFile;

final pubCommandForLogging = [
...pubCommandExecArgs(
useFlutter: workspace.isFlutterWorkspace,
Expand Down Expand Up @@ -203,8 +208,12 @@ mixin _BootstrapMixin on _CleanMixin {
required bool enforceLockfile,
required bool noExample,
}) async {
late final hasLockFile =
File(p.join(package.path, 'pubspec.lock')).existsSync();
final enforceLockfileConfigValue =
workspace.config.commands.bootstrap.enforceLockfile;
final shouldEnforceLockfile =
workspace.config.commands.bootstrap.enforceLockfile || enforceLockfile;
(enforceLockfileConfigValue || enforceLockfile) && hasLockFile;
final command = [
...pubCommandExecArgs(
useFlutter: package.isFlutterPackage,
Expand Down
51 changes: 51 additions & 0 deletions packages/melos/test/commands/bootstrap_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ Generating IntelliJ IDE files...
),
path: path,
),
createLockfile: true,
);

final logger = TestLogger();
Expand All @@ -637,6 +638,56 @@ melos bootstrap
Running "${pubExecArgs.join(' ')} get --enforce-lockfile" in workspace packages...
> SUCCESS
Generating IntelliJ IDE files...
> SUCCESS
-> 0 packages bootstrapped
''',
),
);
});

test('can run pub get --enforce-lockfile without lockfile', () async {
final workspaceDir = await createTemporaryWorkspace(
configBuilder: (path) => MelosWorkspaceConfig.fromYaml(
createYamlMap(
{
'command': {
'bootstrap': {
'enforceLockfile': true,
},
},
},
defaults: configMapDefaults,
),
path: path,
),
);

final logger = TestLogger();
final config = await MelosWorkspaceConfig.fromWorkspaceRoot(workspaceDir);
final workspace = await MelosWorkspace.fromConfig(
config,
logger: logger.toMelosLogger(),
);
final melos = Melos(logger: logger, config: config);
final pubExecArgs = pubCommandExecArgs(
useFlutter: workspace.isFlutterWorkspace,
workspace: workspace,
);

await runMelosBootstrap(melos, logger);

expect(
logger.output,
ignoringAnsii(
'''
melos bootstrap
└> ${workspaceDir.path}
Running "${pubExecArgs.join(' ')} get" in workspace packages...
> SUCCESS
Generating IntelliJ IDE files...
> SUCCESS
Expand Down
8 changes: 8 additions & 0 deletions packages/melos/test/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ MelosWorkspaceConfig _defaultWorkspaceConfigBuilder(String path) =>
Future<Directory> createTemporaryWorkspace({
TestWorkspaceConfigBuilder configBuilder = _defaultWorkspaceConfigBuilder,
bool runPubGet = false,
bool createLockfile = false,
}) async {
final tempDir = createTempDir(p.join(Directory.current.path, '.dart_tool'));
addTearDown(() => deleteEntry(tempDir));
Expand All @@ -128,6 +129,7 @@ Future<Directory> createTemporaryWorkspace({
},
),
path: '.',
createLockfile: createLockfile,
);

if (runPubGet) {
Expand All @@ -149,6 +151,7 @@ Future<Directory> createProject(
Directory workspace,
PubSpec partialPubSpec, {
String? path,
bool createLockfile = false,
}) async {
final pubSpec = partialPubSpec.environment != null
? partialPubSpec
Expand Down Expand Up @@ -179,6 +182,11 @@ Future<Directory> createProject(

await pubSpec.save(projectDirectory);

if (createLockfile) {
final lockfile = p.join(projectDirectory.path, 'pubspec.lock');
writeTextFile(lockfile, '');
}

// Reach into unParsedYaml and determine whether this is a plugin that
// supports Android.
// If it is, create an empty main class file to appease flutter pub
Expand Down

0 comments on commit be94ada

Please sign in to comment.