Commit 779eea68 authored by Ralph Bergmann's avatar Ralph Bergmann Committed by Todd Volkert

make tests in xcodeproj_test.dart and gradle_test.dart hermetic (#18093) (#18548)

parent 5cbbd283
......@@ -4,10 +4,16 @@
import 'dart:async';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/android/gradle.dart';
import 'package:flutter_tools/src/artifacts.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/flutter_manifest.dart';
import 'package:flutter_tools/src/ios/xcodeproj.dart';
import 'package:mockito/mockito.dart';
import 'package:platform/platform.dart';
import 'package:process/process.dart';
import 'package:test/test.dart';
import '../src/common.dart';
......@@ -132,24 +138,25 @@ someOtherProperty: someOtherValue
});
group('Gradle local.properties', () {
Directory temp;
MockLocalEngineArtifacts mockArtifacts;
MockProcessManager mockProcessManager;
FakePlatform android;
FileSystem fs;
setUp(() {
Cache.disableLocking();
temp = fs.systemTempDirectory.createTempSync('flutter_tools');
fs = new MemoryFileSystem();
mockArtifacts = new MockLocalEngineArtifacts();
mockProcessManager = new MockProcessManager();
android = fakePlatform('android');
});
tearDown(() {
temp.deleteSync(recursive: true);
});
Future<String> createMinimalProject(String manifest) async {
final Directory directory = temp.childDirectory('android_project');
final File manifestFile = directory.childFile('pubspec.yaml');
manifestFile.createSync(recursive: true);
manifestFile.writeAsStringSync(manifest);
return directory.path;
void testUsingAndroidContext(String description, dynamic testMethod()) {
testUsingContext(description, testMethod, overrides: <Type, Generator>{
Artifacts: () => mockArtifacts,
ProcessManager: () => mockProcessManager,
Platform: () => android,
FileSystem: () => fs,
});
}
String propertyFor(String key, File file) {
......@@ -166,14 +173,21 @@ someOtherProperty: someOtherValue
String expectedBuildName,
String expectedBuildNumber,
}) async {
final String projectPath = await createMinimalProject(manifest);
when(mockArtifacts.getArtifactPath(Artifact.flutterFramework, TargetPlatform.android_arm, any)).thenReturn('engine');
when(mockArtifacts.engineOutPath).thenReturn(fs.path.join('out', 'android_arm'));
try {
await updateLocalProperties(projectPath: projectPath, buildInfo: buildInfo);
final File manifestFile = fs.file('path/to/project/pubspec.yaml');
manifestFile.createSync(recursive: true);
manifestFile.writeAsStringSync(manifest);
final String propertiesPath = fs.path.join(projectPath, 'android', 'local.properties');
final File localPropertiesFile = fs.file(propertiesPath);
// write schemaData otherwise pubspec.yaml file can't be loaded
const String schemaData = '{}';
writeSchemaFile(fs, schemaData);
try {
await updateLocalProperties(projectPath: 'path/to/project', buildInfo: buildInfo);
final File localPropertiesFile = fs.file('path/to/project/android/local.properties');
expect(propertyFor('flutter.versionName', localPropertiesFile), expectedBuildName);
expect(propertyFor('flutter.versionCode', localPropertiesFile), expectedBuildNumber);
} on Exception {
......@@ -181,7 +195,7 @@ someOtherProperty: someOtherValue
}
}
testUsingContext('extract build name and number from pubspec.yaml', () async {
testUsingAndroidContext('extract build name and number from pubspec.yaml', () async {
const String manifest = '''
name: test
version: 1.0.0+1
......@@ -200,7 +214,7 @@ flutter:
);
});
testUsingContext('extract build name from pubspec.yaml', () async {
testUsingAndroidContext('extract build name from pubspec.yaml', () async {
const String manifest = '''
name: test
version: 1.0.0
......@@ -218,7 +232,7 @@ flutter:
);
});
testUsingContext('allow build info to override build name', () async {
testUsingAndroidContext('allow build info to override build name', () async {
const String manifest = '''
name: test
version: 1.0.0+1
......@@ -236,7 +250,7 @@ flutter:
);
});
testUsingContext('allow build info to override build number', () async {
testUsingAndroidContext('allow build info to override build number', () async {
const String manifest = '''
name: test
version: 1.0.0+1
......@@ -254,7 +268,7 @@ flutter:
);
});
testUsingContext('allow build info to override build name and number', () async {
testUsingAndroidContext('allow build info to override build name and number', () async {
const String manifest = '''
name: test
version: 1.0.0+1
......@@ -272,7 +286,7 @@ flutter:
);
});
testUsingContext('allow build info to override build name and set number', () async {
testUsingAndroidContext('allow build info to override build name and set number', () async {
const String manifest = '''
name: test
version: 1.0.0
......@@ -290,7 +304,7 @@ flutter:
);
});
testUsingContext('allow build info to set build name and number', () async {
testUsingAndroidContext('allow build info to set build name and number', () async {
const String manifest = '''
name: test
dependencies:
......@@ -308,3 +322,21 @@ flutter:
});
});
}
void writeSchemaFile(FileSystem filesystem, String schemaData) {
final String schemaPath = buildSchemaPath(filesystem);
final File schemaFile = filesystem.file(schemaPath);
final String schemaDir = buildSchemaDir(filesystem);
filesystem.directory(schemaDir).createSync(recursive: true);
filesystem.file(schemaFile).writeAsStringSync(schemaData);
}
Platform fakePlatform(String name) {
return new FakePlatform.fromPlatform(const LocalPlatform())..operatingSystem = name;
}
class MockLocalEngineArtifacts extends Mock implements LocalEngineArtifacts {}
class MockProcessManager extends Mock implements ProcessManager {}
class MockXcodeProjectInterpreter extends Mock implements XcodeProjectInterpreter {}
......@@ -9,11 +9,9 @@ import 'package:flutter_tools/src/artifacts.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/bundle.dart' as bundle;
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/flutter_manifest.dart';
import 'package:flutter_tools/src/project.dart';
import 'package:flutter_tools/src/ios/xcodeproj.dart';
import 'package:flutter_tools/src/project.dart';
import 'package:mockito/mockito.dart';
import 'package:platform/platform.dart';
import 'package:process/process.dart';
......@@ -377,28 +375,6 @@ Information about project "Runner":
final String contents = config.readAsStringSync();
expect(contents.contains('ARCHS=arm64'), isTrue);
});
});
group('Xcode Generated.xcconfig', () {
Directory temp;
setUp(() {
Cache.disableLocking();
temp = fs.systemTempDirectory.createTempSync('flutter_tools');
});
tearDown(() {
temp.deleteSync(recursive: true);
});
Future<String> createMinimalProject(String manifest) async {
final Directory directory = temp.childDirectory('ios_project');
final File manifestFile = directory.childFile('pubspec.yaml');
manifestFile.createSync(recursive: true);
manifestFile.writeAsStringSync(manifest);
return directory.path;
}
String propertyFor(String key, File file) {
final List<String> properties = file
......@@ -409,32 +385,48 @@ Information about project "Runner":
return properties.isEmpty ? null : properties.first;
}
void writeSchemaFile(FileSystem filesystem, String schemaData) {
final String schemaPath = buildSchemaPath(filesystem);
final File schemaFile = filesystem.file(schemaPath);
final String schemaDir = buildSchemaDir(filesystem);
filesystem.directory(schemaDir).createSync(recursive: true);
filesystem.file(schemaFile).writeAsStringSync(schemaData);
}
Future<void> checkBuildVersion({
String manifestString,
BuildInfo buildInfo,
String expectedBuildName,
String expectedBuildNumber,
}) async {
final String projectPath = await createMinimalProject(manifestString);
when(mockArtifacts.getArtifactPath(Artifact.flutterFramework, TargetPlatform.ios, any)).thenReturn('engine');
when(mockArtifacts.engineOutPath).thenReturn(fs.path.join('out', 'ios'));
final File manifestFile = fs.file('path/to/project/pubspec.yaml');
manifestFile.createSync(recursive: true);
manifestFile.writeAsStringSync(manifestString);
// write schemaData otherwise pubspec.yaml file can't be loaded
const String schemaData = '{}';
writeSchemaFile(fs, schemaData);
final FlutterManifest manifest =
await new FlutterProject.fromPath(projectPath).manifest;
await new FlutterProject.fromPath('path/to/project').manifest;
updateGeneratedXcodeProperties(
projectPath: projectPath,
projectPath: 'path/to/project',
manifest: manifest,
buildInfo: buildInfo,
targetOverride: bundle.defaultMainPath,
previewDart2: false,
);
final String propertiesPath = fs.path.join(projectPath, 'ios', 'Flutter', 'Generated.xcconfig');
final File localPropertiesFile = fs.file(propertiesPath);
final File localPropertiesFile = fs.file('path/to/project/ios/Flutter/Generated.xcconfig');
expect(propertyFor('FLUTTER_BUILD_NAME', localPropertiesFile), expectedBuildName);
expect(propertyFor('FLUTTER_BUILD_NUMBER', localPropertiesFile), expectedBuildNumber);
}
testUsingContext('extract build name and number from pubspec.yaml', () async {
testUsingOsxContext('extract build name and number from pubspec.yaml', () async {
const String manifest = '''
name: test
version: 1.0.0+1
......@@ -453,7 +445,7 @@ flutter:
);
});
testUsingContext('extract build name from pubspec.yaml', () async {
testUsingOsxContext('extract build name from pubspec.yaml', () async {
const String manifest = '''
name: test
version: 1.0.0
......@@ -471,7 +463,7 @@ flutter:
);
});
testUsingContext('allow build info to override build name', () async {
testUsingOsxContext('allow build info to override build name', () async {
const String manifest = '''
name: test
version: 1.0.0+1
......@@ -489,7 +481,7 @@ flutter:
);
});
testUsingContext('allow build info to override build number', () async {
testUsingOsxContext('allow build info to override build number', () async {
const String manifest = '''
name: test
version: 1.0.0+1
......@@ -507,7 +499,7 @@ flutter:
);
});
testUsingContext('allow build info to override build name and number', () async {
testUsingOsxContext('allow build info to override build name and number', () async {
const String manifest = '''
name: test
version: 1.0.0+1
......@@ -525,7 +517,7 @@ flutter:
);
});
testUsingContext('allow build info to override build name and set number', () async {
testUsingOsxContext('allow build info to override build name and set number', () async {
const String manifest = '''
name: test
version: 1.0.0
......@@ -543,7 +535,7 @@ flutter:
);
});
testUsingContext('allow build info to set build name and number', () async {
testUsingOsxContext('allow build info to set build name and number', () async {
const String manifest = '''
name: test
dependencies:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment