Skip to content

Commit

Permalink
fix: throw exception when using "melos exec" in "run" along with "exe…
Browse files Browse the repository at this point in the history
…c" configuration
  • Loading branch information
BenVercammen committed Dec 22, 2022
1 parent f8e1551 commit 268a6dd
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/melos/lib/src/commands/exec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ mixin _ExecMixin on _Melos {
final execArgsString = execArgs.join(' ');
final prefixLogs = concurrency != 1 && packages.length != 1;

if (execArgsString.contains('melos exec')) {
throw InvalidScriptConfigException(
execArgsString,
'Do not use "melos exec" in "run" when also defining "exec"',
);
}

logger.command('melos exec', withDollarSign: true);
logger
.child(targetStyle(execArgsString))
Expand Down
15 changes: 15 additions & 0 deletions packages/melos/lib/src/common/exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,18 @@ class RestrictedBranchException implements MelosException {
'to only be allowed to run on the "$allowedBranch" but the current '
'branch is "$currentBranch".';
}

class InvalidScriptConfigException implements MelosException {
InvalidScriptConfigException(this.execArgsString, this.message);

final String execArgsString;

final String message;

@override
String toString() {
return 'InvalidScriptConfigException: '
'$message\n'
'\trun: "$execArgsString"';
}
}
58 changes: 58 additions & 0 deletions packages/melos/test/commands/run_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,63 @@ melos run test_script
),
);
});

test('throw exception when "melos exec" with "exec" options', () async {
final workspaceDir = createTemporaryWorkspaceDirectory(
configBuilder: (path) => MelosWorkspaceConfig(
path: path,
name: 'test_package',
packages: [
createGlob('packages/**', currentDirectoryPath: path),
],
scripts: Scripts({
'test_script': Script(
name: 'test_script',
run: 'melos exec "echo hello"',
exec: ExecOptions(
concurrency: 1,
),
)
}),
),
);

await createProject(
workspaceDir,
const PubSpec(name: 'a'),
);

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

try {
await melos.run(scriptName: 'test_script', noSelect: true);
fail('Using "melos exec" in "run" with "exec" should fail.');
} on Exception {
expect(
logger.output.normalizeNewLines(),
ignoringAnsii(
'''
melos run test_script
└> melos exec --concurrency 1 -- "melos exec \\"echo hello\\""
└> RUNNING
e-InvalidScriptConfigException: Do not use "melos exec" in "run" when also defining "exec"
e-\trun: "melos exec "echo hello""
e-
e-
melos run test_script
└> melos exec --concurrency 1 -- "melos exec \\"echo hello\\""
└> FAILED
''',
),
);
}
});
});
}

0 comments on commit 268a6dd

Please sign in to comment.