Skip to content

Commit

Permalink
feat(shorebird_cli): improve "shorebird not initialized" error messag…
Browse files Browse the repository at this point in the history
…es (#1779)
  • Loading branch information
bryanoltman authored Mar 7, 2024
1 parent c0a2323 commit 94c4b69
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 102 deletions.
5 changes: 0 additions & 5 deletions packages/shorebird_cli/lib/src/shorebird_env.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,6 @@ class ShorebirdEnv {
}
}

/// Whether `shorebird init` has been run in the current project.
bool get isShorebirdInitialized {
return hasShorebirdYaml && pubspecContainsShorebirdYaml;
}

/// Whether the current project has a `shorebird.yaml` file.
bool get hasShorebirdYaml => getShorebirdYaml() != null;

Expand Down
31 changes: 26 additions & 5 deletions packages/shorebird_cli/lib/src/shorebird_validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,32 @@ class ShorebirdValidator {
throw UserNotAuthorizedException();
}

if (checkShorebirdInitialized && !shorebirdEnv.isShorebirdInitialized) {
logger.err(
'Shorebird is not initialized. Did you run "shorebird init"?',
);
throw ShorebirdNotInitializedException();
if (checkShorebirdInitialized) {
if (!shorebirdEnv.hasShorebirdYaml) {
logger
..err(
'''Unable to find shorebird.yaml. Are you in a shorebird app directory?''',
)
..info(
'''If you have not yet initialized your app, run ${lightCyan.wrap('shorebird init')} to get started.''',
);
throw ShorebirdNotInitializedException();
}

if (!shorebirdEnv.pubspecContainsShorebirdYaml) {
logger
..err(
'''Your pubspec.yaml does not have shorebird.yaml as a flutter asset.''',
)
..info('''
To fix, update your pubspec.yaml to include the following:
flutter:
assets:
- shorebird.yaml # Add this line
''');
throw ShorebirdNotInitializedException();
}
}

for (final validator in validators) {
Expand Down
75 changes: 0 additions & 75 deletions packages/shorebird_cli/test/src/shorebird_env_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -372,81 +372,6 @@ flutter:
});
});

group('isShorebirdInitialized', () {
test('returns false when shorebird.yaml does not exist', () {
final tempDir = Directory('temp');
expect(
IOOverrides.runZoned(
() => runWithOverrides(() => shorebirdEnv.isShorebirdInitialized),
getCurrentDirectory: () => tempDir,
),
isFalse,
);
});

test(
'returns false when shorebird.yaml exists '
'but pubspec does not contain shorebird.yaml', () {
final tempDir = Directory.systemTemp.createTempSync();
File(
p.join(tempDir.path, 'shorebird.yaml'),
).writeAsStringSync('app_id: test-app-id');
File(
p.join(tempDir.path, 'pubspec.yaml'),
).writeAsStringSync('name: test');
expect(
IOOverrides.runZoned(
() => runWithOverrides(() => shorebirdEnv.isShorebirdInitialized),
getCurrentDirectory: () => tempDir,
),
isFalse,
);
});

test(
'returns false when shorebird.yaml does not exist '
'but pubspec contains shorebird.yaml', () {
final tempDir = Directory.systemTemp.createTempSync();
File(
p.join(tempDir.path, 'pubspec.yaml'),
).writeAsStringSync('''
name: test
flutter:
assets:
- shorebird.yaml''');
expect(
IOOverrides.runZoned(
() => runWithOverrides(() => shorebirdEnv.isShorebirdInitialized),
getCurrentDirectory: () => tempDir,
),
isFalse,
);
});

test(
'returns true when shorebird.yaml exists '
'and pubspec contains shorebird.yaml', () {
final tempDir = Directory.systemTemp.createTempSync();
File(
p.join(tempDir.path, 'shorebird.yaml'),
).writeAsStringSync('app_id: test-app-id');
File(
p.join(tempDir.path, 'pubspec.yaml'),
).writeAsStringSync('''
name: test
flutter:
assets:
- shorebird.yaml''');
expect(
IOOverrides.runZoned(
() => runWithOverrides(() => shorebirdEnv.isShorebirdInitialized),
getCurrentDirectory: () => tempDir,
),
isTrue,
);
});
});

group('flutterRevision', () {
test('returns correct revision', () {
const revision = 'test-revision';
Expand Down
81 changes: 64 additions & 17 deletions packages/shorebird_cli/test/src/shorebird_validator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,70 @@ void main() {
]);
});

test(
'throws ShorebirdNotInitializedException '
'when shorebird has not been initialized', () async {
when(() => shorebirdEnv.isShorebirdInitialized).thenReturn(false);
await expectLater(
runWithOverrides(
() => shorebirdValidator.validatePreconditions(
checkShorebirdInitialized: true,
),
),
throwsA(isA<ShorebirdNotInitializedException>()),
);
verify(
() => logger.err(
'Shorebird is not initialized. Did you run "shorebird init"?',
),
).called(1);
group(
'''when shorebird has not been properly initialized for the current app''',
() {
group("when shorebird.yaml doesn't exist", () {
setUp(() {
when(() => shorebirdEnv.hasShorebirdYaml).thenReturn(false);
});

test(
'''prints error message and throws ShorebirdNotInitializedException''',
() async {
await expectLater(
runWithOverrides(
() => shorebirdValidator.validatePreconditions(
checkShorebirdInitialized: true,
),
),
throwsA(isA<ShorebirdNotInitializedException>()),
);
verifyInOrder([
() => logger.err(
'''Unable to find shorebird.yaml. Are you in a shorebird app directory?''',
),
() => logger.info(
'''If you have not yet initialized your app, run ${lightCyan.wrap('shorebird init')} to get started.''',
),
]);
});
});

group("when pubspec.yaml doesn't contain shorebird.yaml as an asset",
() {
setUp(() {
when(() => shorebirdEnv.hasShorebirdYaml).thenReturn(true);
when(() => shorebirdEnv.pubspecContainsShorebirdYaml).thenReturn(
false,
);
});

test(
'''prints error message and throws ShorebirdNotInitializedException''',
() async {
await expectLater(
runWithOverrides(
() => shorebirdValidator.validatePreconditions(
checkShorebirdInitialized: true,
),
),
throwsA(isA<ShorebirdNotInitializedException>()),
);
verifyInOrder([
() => logger.err(
'''Your pubspec.yaml does not have shorebird.yaml as a flutter asset.''',
),
() => logger.info('''
To fix, update your pubspec.yaml to include the following:
flutter:
assets:
- shorebird.yaml # Add this line
'''),
]);
});
});
});

test('throws ValidationFailedException if validator fails', () async {
Expand Down

0 comments on commit 94c4b69

Please sign in to comment.