Skip to content

Commit

Permalink
refactor(shorebird_cli): ShorebirdFlutterValidator use `ShorebirdFl…
Browse files Browse the repository at this point in the history
…utterManager`
  • Loading branch information
felangel committed Aug 8, 2023
1 parent 58fd799 commit c1095a5
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 20 deletions.
9 changes: 9 additions & 0 deletions packages/shorebird_cli/lib/src/shorebird_flutter_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,13 @@ class ShorebirdFlutterManager {
directory: _workingDirectory(revision: revision),
);
}

/// Whether the current revision is porcelain (unmodified).
Future<bool> isPorcelain({String? revision}) async {
final status = await git.status(
directory: _workingDirectory(revision: revision),
args: ['--untracked-files=no', '--porcelain'],
);
return status.isEmpty;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:shorebird_cli/src/platform.dart';
import 'package:shorebird_cli/src/process.dart';
import 'package:shorebird_cli/src/shorebird_env.dart';
import 'package:shorebird_cli/src/shorebird_flutter_manager.dart';
import 'package:shorebird_cli/src/validators/validators.dart';
import 'package:version/version.dart';

Expand Down Expand Up @@ -40,7 +41,7 @@ class ShorebirdFlutterValidator extends Validator {
);
}

if (!await _flutterDirectoryIsClean(process)) {
if (!await shorebirdFlutterManager.isPorcelain()) {
issues.add(
ValidationIssue(
severity: ValidationIssueSeverity.warning,
Expand Down Expand Up @@ -112,15 +113,6 @@ This can cause unexpected behavior if you are switching between the tools and th
return issues;
}

Future<bool> _flutterDirectoryIsClean(ShorebirdProcess process) async {
final result = await process.run(
'git',
['status', '--untracked-files=no', '--porcelain'],
workingDirectory: shorebirdEnv.flutterDirectory.path,
);
return result.stdout.toString().trim().isEmpty;
}

Future<String> _shorebirdFlutterVersion(ShorebirdProcess process) {
return _getFlutterVersion(process: process);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ void main() {
directory: any(named: 'directory'),
),
).thenAnswer((_) async {});
when(
() => git.status(
directory: p.join(flutterDirectory.parent.path, flutterRevision),
args: ['--untracked-files=no', '--porcelain'],
),
).thenAnswer((_) async => '');
when(() => shorebirdEnv.flutterDirectory).thenReturn(flutterDirectory);
when(() => shorebirdEnv.flutterRevision).thenReturn(flutterRevision);
});
Expand Down Expand Up @@ -210,5 +216,68 @@ void main() {
);
});
});

group('isPorcelain', () {
test('returns true when status is empty', () async {
await expectLater(
runWithOverrides(() => shorebirdFlutterManager.isPorcelain()),
completion(isTrue),
);
verify(
() => git.status(
directory: p.join(flutterDirectory.parent.path, flutterRevision),
args: ['--untracked-files=no', '--porcelain'],
),
).called(1);
});

test('returns false when status is not empty', () async {
when(
() => git.status(
directory: any(named: 'directory'),
args: any(named: 'args'),
),
).thenAnswer((_) async => 'M some/file');
await expectLater(
runWithOverrides(() => shorebirdFlutterManager.isPorcelain()),
completion(isFalse),
);
verify(
() => git.status(
directory: p.join(flutterDirectory.parent.path, flutterRevision),
args: ['--untracked-files=no', '--porcelain'],
),
).called(1);
});

test('throws ProcessException when git command exits non-zero code',
() async {
const errorMessage = 'oh no!';
when(
() => git.status(
directory: any(named: 'directory'),
args: any(named: 'args'),
),
).thenThrow(
ProcessException(
'git',
['status'],
errorMessage,
ExitCode.software.code,
),
);

expect(
runWithOverrides(() => shorebirdFlutterManager.isPorcelain()),
throwsA(
isA<ProcessException>().having(
(e) => e.message,
'message',
errorMessage,
),
),
);
});
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:scoped/scoped.dart';
import 'package:shorebird_cli/src/platform.dart';
import 'package:shorebird_cli/src/process.dart';
import 'package:shorebird_cli/src/shorebird_env.dart';
import 'package:shorebird_cli/src/shorebird_flutter_manager.dart';
import 'package:shorebird_cli/src/validators/validators.dart';
import 'package:test/test.dart';

Expand All @@ -16,6 +17,9 @@ class _MockPlatform extends Mock implements Platform {}

class _MockShorebirdEnv extends Mock implements ShorebirdEnv {}

class _MockShorebirdFlutterManager extends Mock
implements ShorebirdFlutterManager {}

class _MockShorebirdProcess extends Mock implements ShorebirdProcess {}

void main() {
Expand All @@ -40,9 +44,9 @@ Tools • Dart 2.19.6 • DevTools 2.20.1
late Directory tempDir;
late ShorebirdProcessResult pathFlutterVersionProcessResult;
late ShorebirdProcessResult shorebirdFlutterVersionProcessResult;
late ShorebirdProcessResult gitStatusProcessResult;
late ShorebirdProcess shorebirdProcess;
late ShorebirdEnv shorebirdEnv;
late ShorebirdFlutterManager shorebirdFlutterManager;
late Platform platform;

R runWithOverrides<R>(R Function() body) {
Expand All @@ -52,6 +56,9 @@ Tools • Dart 2.19.6 • DevTools 2.20.1
platformRef.overrideWith(() => platform),
processRef.overrideWith(() => shorebirdProcess),
shorebirdEnvRef.overrideWith(() => shorebirdEnv),
shorebirdFlutterManagerRef.overrideWith(
() => shorebirdFlutterManager,
),
},
);
}
Expand All @@ -73,6 +80,7 @@ Tools • Dart 2.19.6 • DevTools 2.20.1
tempDir = setupTempDirectory();
platform = _MockPlatform();
shorebirdEnv = _MockShorebirdEnv();
shorebirdFlutterManager = _MockShorebirdFlutterManager();

when(() => shorebirdEnv.flutterRevision).thenReturn(flutterRevision);
when(
Expand All @@ -83,17 +91,14 @@ Tools • Dart 2.19.6 • DevTools 2.20.1

pathFlutterVersionProcessResult = _MockProcessResult();
shorebirdFlutterVersionProcessResult = _MockProcessResult();
gitStatusProcessResult = _MockProcessResult();
shorebirdProcess = _MockShorebirdProcess();

validator = ShorebirdFlutterValidator();
when(
() => shorebirdProcess.run(
'git',
['status', '--untracked-files=no', '--porcelain'],
workingDirectory: any(named: 'workingDirectory'),
() => shorebirdFlutterManager.isPorcelain(
revision: any(named: 'revision'),
),
).thenAnswer((_) async => gitStatusProcessResult);
).thenAnswer((_) async => true);
when(
() => shorebirdProcess.run(
'flutter',
Expand All @@ -118,7 +123,6 @@ Tools • Dart 2.19.6 • DevTools 2.20.1
.thenReturn(shorebirdFlutterVersionMessage);
when(() => shorebirdFlutterVersionProcessResult.stderr).thenReturn('');
when(() => shorebirdFlutterVersionProcessResult.exitCode).thenReturn(0);
when(() => gitStatusProcessResult.stdout).thenReturn('');
});

test('has a non-empty description', () {
Expand Down Expand Up @@ -146,8 +150,11 @@ Tools • Dart 2.19.6 • DevTools 2.20.1
});

test('warns when Flutter has local modifications', () async {
when(() => gitStatusProcessResult.stdout)
.thenReturn('Changes not staged for commit');
when(
() => shorebirdFlutterManager.isPorcelain(
revision: any(named: 'revision'),
),
).thenAnswer((_) async => false);

final results = await runWithOverrides(validator.validate);

Expand Down

0 comments on commit c1095a5

Please sign in to comment.