Skip to content

Commit

Permalink
create PubspecReader and use it (#868)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekpacia authored Feb 5, 2023
1 parent 380f6c5 commit efd1cd2
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 14 deletions.
8 changes: 8 additions & 0 deletions packages/patrol/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ dev_dependencies:

flutter:
uses-material-design: true

patrol:
android:
package_name: pl.leancode.patrol.example
app_name: Patrol example
ios:
bundle_id: pl.leancode.patrol.Example
app_name: Patrol example
13 changes: 9 additions & 4 deletions packages/patrol_cli/lib/src/command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import 'package:patrol_cli/src/features/test/android_test_backend.dart';
import 'package:patrol_cli/src/features/test/ios_deploy.dart';
import 'package:patrol_cli/src/features/test/ios_test_backend.dart';
import 'package:patrol_cli/src/features/test/native_test_runner.dart';
import 'package:patrol_cli/src/features/test/pubspec_reader.dart';
import 'package:patrol_cli/src/features/test/test_command.dart';
import 'package:patrol_cli/src/features/update/update_command.dart';
import 'package:platform/platform.dart';
Expand Down Expand Up @@ -126,6 +127,14 @@ class PatrolCommandRunner extends CommandRunner<int> {
fs: _fs,
),
testRunner: NativeTestRunner(),
dartDefinesReader: DartDefinesReader(
projectRoot: _fs.currentDirectory,
fs: _fs,
),
pubspecReader: PubspecReader(
projectRoot: _fs.currentDirectory,
fs: _fs,
),
androidTestBackend: AndroidTestBackend(
adb: Adb(),
processManager: LoggingLocalProcessManager(logger: _logger),
Expand All @@ -146,10 +155,6 @@ class PatrolCommandRunner extends CommandRunner<int> {
parentDisposeScope: _disposeScope,
logger: _logger,
),
dartDefinesReader: DartDefinesReader(
projectRoot: _fs.currentDirectory,
fs: _fs,
),
parentDisposeScope: _disposeScope,
logger: _logger,
),
Expand Down
84 changes: 84 additions & 0 deletions packages/patrol_cli/lib/src/features/test/pubspec_reader.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import 'package:file/file.dart';
import 'package:path/path.dart' show join;
import 'package:yaml/yaml.dart';

class PatrolPubspecConfig {
PatrolPubspecConfig({this.android, this.ios});

AndroidPubspecConfig? android;
IOSPubspecConfig? ios;
}

class AndroidPubspecConfig {
AndroidPubspecConfig({this.packageName, this.appName});
String? packageName;
String? appName;
}

class IOSPubspecConfig {
IOSPubspecConfig({this.bundleId, this.appName});

String? bundleId;
String? appName;
}

/// Reads Patrol CLI configuration block from pubspec.yaml.
class PubspecReader {
const PubspecReader({
required Directory projectRoot,
required FileSystem fs,
}) : _projectRoot = projectRoot,
_fs = fs;

final Directory _projectRoot;
final FileSystem _fs;

PatrolPubspecConfig read() {
final filePath = join(_projectRoot.path, 'pubspec.yaml');
final file = _fs.file(filePath);

if (!file.existsSync()) {
throw FileSystemException("pubspec.yaml doesn't exist", filePath);
}

final contents = file.readAsStringSync();
final yaml = loadYaml(contents) as Map;

final config = PatrolPubspecConfig();
final patrol = yaml['patrol'] as Map?;
if (patrol != null) {
final patrol = yaml['patrol'] as Map;
final android = patrol['android'] as Map?;
if (android != null) {
final androidConfig = AndroidPubspecConfig();
config.android = androidConfig;

final dynamic packageName = android['package_name'];
if (packageName != null && packageName is String?) {
androidConfig.packageName = packageName;
}
final dynamic appName = android['app_name'];
if (appName != null && appName is String?) {
androidConfig.appName = appName;
}
}

final ios = patrol['ios'] as Map?;
if (ios != null) {
final iosConfig = IOSPubspecConfig();
config.ios = iosConfig;

final dynamic bundleId = ios['bundle_id'];
if (bundleId != null && bundleId is String?) {
iosConfig.bundleId = bundleId;
}
final dynamic appName = ios['app_name'];
if (appName != null && appName is String?) {
iosConfig.appName = appName;
}
}
}

return config;
}
}
23 changes: 15 additions & 8 deletions packages/patrol_cli/lib/src/features/test/test_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:patrol_cli/src/features/run_commons/test_finder.dart';
import 'package:patrol_cli/src/features/test/android_test_backend.dart';
import 'package:patrol_cli/src/features/test/ios_test_backend.dart';
import 'package:patrol_cli/src/features/test/native_test_runner.dart';
import 'package:patrol_cli/src/features/test/pubspec_reader.dart';

import '../../common/staged_command.dart';
import '../../common/tool_exit.dart';
Expand Down Expand Up @@ -46,16 +47,18 @@ class TestCommand extends StagedCommand<TestCommandConfig> {
required TestFinder testFinder,
required NativeTestRunner testRunner,
required DartDefinesReader dartDefinesReader,
required PubspecReader pubspecReader,
required AndroidTestBackend androidTestBackend,
required IOSTestBackend iosTestBackend,
required DisposeScope parentDisposeScope,
required Logger logger,
}) : _deviceFinder = deviceFinder,
_testFinder = testFinder,
_testRunner = testRunner,
_dartDefinesReader = dartDefinesReader,
_pubspecReader = pubspecReader,
_androidTestBackend = androidTestBackend,
_iosTestBackend = iosTestBackend,
_dartDefinesReader = dartDefinesReader,
_logger = logger {
_testRunner.disposedBy(parentDisposeScope);

Expand Down Expand Up @@ -135,9 +138,10 @@ class TestCommand extends StagedCommand<TestCommandConfig> {
final DeviceFinder _deviceFinder;
final TestFinder _testFinder;
final NativeTestRunner _testRunner;
final DartDefinesReader _dartDefinesReader;
final PubspecReader _pubspecReader;
final AndroidTestBackend _androidTestBackend;
final IOSTestBackend _iosTestBackend;
final DartDefinesReader _dartDefinesReader;

final Logger _logger;

Expand All @@ -162,8 +166,10 @@ class TestCommand extends StagedCommand<TestCommandConfig> {
}

final flavor = argResults?['flavor'] as String?;
final pubspecConfig = _pubspecReader.read();

final devices = argResults?['device'] as List<String>? ?? [];
final attachedDevices = await _deviceFinder.find(devices);

final dartDefines = {
..._dartDefinesReader.fromFile(),
Expand All @@ -177,8 +183,11 @@ class TestCommand extends StagedCommand<TestCommandConfig> {
_logger.detail('Received --dart-define: ${dartDefine.key}');
}

final dynamic packageName = argResults?['package-name'];
final dynamic bundleId = argResults?['bundle-id'];
var packageName = argResults?['package-name'] as String?;
packageName ??= pubspecConfig.android?.packageName;

var bundleId = argResults?['bundle-id'] as String?;
bundleId ??= pubspecConfig.ios?.bundleId;

final dynamic wait = argResults?['wait'];
if (wait != null && int.tryParse(wait as String) == null) {
Expand All @@ -203,8 +212,6 @@ class TestCommand extends StagedCommand<TestCommandConfig> {
_logger.info('Every test target will be run $repeat times');
}

final attachedDevices = await _deviceFinder.find(devices);

return TestCommandConfig(
devices: attachedDevices,
targets: targets,
Expand All @@ -218,8 +225,8 @@ class TestCommand extends StagedCommand<TestCommandConfig> {
dartDefines: <String, String?>{
...dartDefines,
envWaitKey: wait as String? ?? '0',
envPackageNameKey: packageName as String?,
envBundleIdKey: bundleId as String?,
envPackageNameKey: packageName,
envBundleIdKey: bundleId,
envVerbose: '$verbose',
}.withNullsRemoved(),
packageName: packageName,
Expand Down
9 changes: 7 additions & 2 deletions packages/patrol_cli/test/features/test/test_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:patrol_cli/src/features/run_commons/test_finder.dart';
import 'package:patrol_cli/src/features/test/android_test_backend.dart';
import 'package:patrol_cli/src/features/test/ios_test_backend.dart';
import 'package:patrol_cli/src/features/test/native_test_runner.dart';
import 'package:patrol_cli/src/features/test/pubspec_reader.dart';
import 'package:patrol_cli/src/features/test/test_command.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -48,6 +49,9 @@ void main() {
..createSync(recursive: true);
fs.currentDirectory = wd;
final integrationTestDir = fs.directory('integration_test')..createSync();
fs.file('pubspec.yaml')
..createSync()
..writeAsString('name: awesome_app');

deviceFinder = MockDeviceFinder();
when(
Expand All @@ -67,13 +71,14 @@ void main() {
deviceFinder: deviceFinder,
testFinder: testFinder,
testRunner: testRunner,
androidTestBackend: androidTestBackend,
iosTestBackend: iosTestBackend,
dartDefinesReader: DartDefinesReader(
fs: fs,
projectRoot: fs.currentDirectory,
),
pubspecReader: PubspecReader(fs: fs, projectRoot: fs.currentDirectory),
parentDisposeScope: DisposeScope(),
androidTestBackend: androidTestBackend,
iosTestBackend: iosTestBackend,
logger: MockLogger(),
);
});
Expand Down

0 comments on commit efd1cd2

Please sign in to comment.