Skip to content

Commit

Permalink
fix(shorebird_cli): make android manifest and shorebird version valid…
Browse files Browse the repository at this point in the history
…ation more robust (#273)
  • Loading branch information
bryanoltman authored Apr 8, 2023
1 parent 2dc180b commit 7e121b0
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,38 @@ class AndroidInternetPermissionValidator extends Validator {
'src',
),
);
final manifestsWithoutInternetPermission = androidSrcDir

if (!androidSrcDir.existsSync()) {
return [
const ValidationIssue(
severity: ValidationIssueSeverity.error,
message: 'No Android project found',
),
];
}

final manifestFiles = androidSrcDir
.listSync()
.whereType<Directory>()
.where((dir) {
return dir.listSync().whereType<File>().any(
(file) => p.basename(file.path) == 'AndroidManifest.xml',
);
})
.map((e) => p.join(e.path, manifestFileName))
.where(
(dir) => dir
.listSync()
.whereType<File>()
.any((file) => p.basename(file.path) == 'AndroidManifest.xml'),
)
.map((e) => p.join(e.path, manifestFileName));

if (manifestFiles.isEmpty) {
return [
ValidationIssue(
severity: ValidationIssueSeverity.error,
message:
'No AndroidManifest.xml files found in ${androidSrcDir.path}',
),
];
}

final manifestsWithoutInternetPermission = manifestFiles
.where((manifest) => !_androidManifestHasInternetPermission(manifest));

if (manifestsWithoutInternetPermission.isNotEmpty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,20 @@ class ShorebirdVersionValidator extends Validator {
@override
Future<List<ValidationIssue>> validate() async {
final workingDirectory = p.dirname(Platform.script.toFilePath());
final isShorebirdUpToDate = await isShorebirdVersionCurrent(
workingDirectory: workingDirectory,
);
final bool isShorebirdUpToDate;

try {
isShorebirdUpToDate = await isShorebirdVersionCurrent(
workingDirectory: workingDirectory,
);
} on ProcessException catch (e) {
return [
ValidationIssue(
severity: ValidationIssueSeverity.error,
message: 'Failed to get shorebird version. Error: ${e.message}',
),
];
}

if (!isShorebirdUpToDate) {
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@ void main() {
},
);

test('returns an error if no android project is found', () async {
final results = await AndroidInternetPermissionValidator().validate();

expect(results, hasLength(1));
expect(results.first.severity, ValidationIssueSeverity.error);
expect(results.first.message, 'No Android project found');
});

test('returns an error if no AndroidManifest.xml files are found',
() async {
final tempDirectory = createTempDir();
Directory(p.join(tempDirectory.path, 'android', 'app', 'src', 'debug'))
.createSync(recursive: true);

final results = await IOOverrides.runZoned(
() => AndroidInternetPermissionValidator().validate(),
getCurrentDirectory: () => tempDirectory,
);

expect(results, hasLength(1));
expect(results.first.severity, ValidationIssueSeverity.error);
expect(
results.first.message,
startsWith('No AndroidManifest.xml files found in'),
);
});

test(
'returns separate errors for all AndroidManifest.xml files without the '
'INTERNET permission',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,29 @@ void main() {
contains('A new version of shorebird is available!'),
);
});

test(
'returns an error on failure to retrieve shorebird version',
() async {
when(
() => fetchLatestVersionResult.stdout,
).thenThrow(
const ProcessException(
'git',
['--rev'],
'Some error',
),
);

final results = await validator.validate();

expect(results, hasLength(1));
expect(results.first.severity, ValidationIssueSeverity.error);
expect(
results.first.message,
'Failed to get shorebird version. Error: Some error',
);
},
);
});
}

0 comments on commit 7e121b0

Please sign in to comment.