-
Notifications
You must be signed in to change notification settings - Fork 5
/
local_testing.dart
242 lines (210 loc) · 7.67 KB
/
local_testing.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import 'dart:async';
import 'dart:io';
import 'package:dartx/dartx_io.dart';
import 'package:dcli/dcli.dart';
import 'package:dcli/posix.dart';
import 'package:path/path.dart';
import 'package:sidekick_test/src/download_dart.sh.template.dart';
import 'package:sidekick_test/src/sidekick_config.sh.template.dart';
import 'package:test/test.dart';
/// True when dependencies should be linked to local sidekick dependencies
final bool shouldUseLocalDeps = env['SIDEKICK_PUB_DEPS'] != 'true';
/// Changes the sidekick_core dependency to a local override
void overrideSidekickCoreWithLocalPath(Directory package) {
if (!shouldUseLocalDeps) return;
print('Overriding sidekick_core dependency to local');
// assuming cwd when running those tests is in the sidekick package
final path = canonicalize('../sidekick_core');
_overrideDependency(
package: package,
dependency: 'sidekick_core',
path: path,
);
}
/// Changes the sidekick_plugin_installer dependency to a local override
void overrideSidekickPluginInstallerWithLocalPath(Directory package) {
if (!shouldUseLocalDeps) return;
print('Overriding sidekick_plugin_installer dependency to local');
// assuming cwd when running those tests is in the sidekick package
final path = canonicalize('../sidekick_plugin_installer');
_overrideDependency(
package: package,
dependency: 'sidekick_plugin_installer',
path: path,
);
}
/// Set to true, when the code should be checked for lint warnings and code
/// formatting
///
/// Usually, this should be checked only on the latest dart version, because
/// dartfmt is updated with the sdk and may require different formatting
final bool analyzeGeneratedCode = env['SIDEKICK_ANALYZE'] == 'true';
/// Fakes a sidekick package by writing required files and environment variables
///
/// Optional Parameters:
/// - [overrideSidekickCoreWithLocalDependency] whether to add a dependency
/// override to use the local sidekick_core dependency
/// - [sidekickCoreVersion] the dependency of sidekick_core in the pubspec.
/// Only written to pubspec if value is not null.
/// - [lockedSidekickCoreVersion] the used version in pubspec.lock
/// - [sidekickCliVersion] sidekick: cli_version: <sidekickCliVersion> in the
/// pubspec. Only written to pubspec if value is not null.
R insideFakeProjectWithSidekick<R>(
R Function(Directory projectRoot) callback, {
bool overrideSidekickCoreWithLocalDependency = false,
String? sidekickCoreVersion,
String? lockedSidekickCoreVersion,
String? sidekickCliVersion,
String dartSdkConstraint = '>=2.14.0 <3.0.0',
bool insideGitRepo = false,
}) {
final tempDir = Directory.systemTemp.createTempSync();
Directory projectRoot = tempDir;
if (insideGitRepo) {
'git init -q ${tempDir.path}'.run;
projectRoot = tempDir.directory('myProject')..createSync();
}
projectRoot.file('pubspec.yaml')
..createSync()
..writeAsStringSync('''
name: main_project
environment:
sdk: '>=2.14.0 <3.0.0'
''');
projectRoot.file('dash').createSync();
final fakeSidekickDir = projectRoot.directory('packages/dash')
..createSync(recursive: true);
fakeSidekickDir.file('pubspec.yaml')
..createSync()
..writeAsStringSync('''
name: dash
environment:
sdk: '$dartSdkConstraint'
${sidekickCoreVersion == null && !overrideSidekickCoreWithLocalDependency ? '' : '''
dependencies:
sidekick_core: ${sidekickCoreVersion ?? '0.0.0'}
'''}
${sidekickCliVersion == null ? '' : '''
sidekick:
cli_version: $sidekickCliVersion
'''}
''');
fakeSidekickDir.file('pubspec.lock')
..createSync()
..writeAsStringSync('''
packages:
sidekick_core:
dependency: "direct main"
source: hosted
description:
name: sidekick_core
url: "https://pub.dev"
version: "${lockedSidekickCoreVersion ?? '0.0.0'}"
''');
final fakeSidekickLibDir = fakeSidekickDir.directory('lib')..createSync();
fakeSidekickLibDir.file('src/dash_project.dart').createSync(recursive: true);
fakeSidekickLibDir.file('dash_sidekick.dart').createSync();
// tool dir
final toolDir = fakeSidekickDir.directory('tool')..createSync();
toolDir.file('download_dart.sh')
..createSync(recursive: true)
..writeAsStringSync(downloadDartSh);
final sidekickConfig = toolDir.file('sidekick_config.sh')
..createSync(recursive: true)
..writeAsStringSync(sidekickConfigSh);
chmod(sidekickConfig.path, permission: '755');
env['SIDEKICK_PACKAGE_HOME'] = fakeSidekickDir.absolute.path;
env['SIDEKICK_ENTRYPOINT_HOME'] = projectRoot.absolute.path;
if (!env.exists('SIDEKICK_ENABLE_UPDATE_CHECK')) {
env['SIDEKICK_ENABLE_UPDATE_CHECK'] = 'false';
}
if (overrideSidekickCoreWithLocalDependency) {
overrideSidekickCoreWithLocalPath(fakeSidekickDir);
}
addTearDown(() {
projectRoot.deleteSync(recursive: true);
env['SIDEKICK_PACKAGE_HOME'] = null;
env['SIDEKICK_ENTRYPOINT_HOME'] = null;
env['SIDEKICK_ENABLE_UPDATE_CHECK'] = null;
});
Directory cwd = projectRoot;
// Use FileSystemEntity.typeSync and FileSystemEntity.type of old zone,
// otherwise doesn't work correctly in Dart >= 2.18
final oldZone = Zone.current;
return IOOverrides.runZoned<R>(
() => callback(projectRoot),
getCurrentDirectory: () => cwd,
setCurrentDirectory: (dir) => cwd = Directory(dir),
fseGetTypeSync: (String path, bool followLinks) {
return oldZone.run(
() => FileSystemEntity.typeSync(path, followLinks: followLinks),
);
},
fseGetType: (String path, bool followLinks) {
return oldZone.run(
() => FileSystemEntity.type(path, followLinks: followLinks),
);
},
);
}
/// Links [SidekickDartRuntime] to [_systemDartSdkPath]
///
/// Use when testing a command which depends on [SidekickDartRuntime.dart] with
/// a fake sidekick package
///
/// TODO doesn't work yet for functional sidekick CLI packages because
/// their recompile will kick off and redownload its Dart runtime
void overrideSidekickDartRuntimeWithSystemDartRuntime(Directory sidekick) {
env['SIDEKICK_PACKAGE_HOME'] = sidekick.absolute.path;
final systemDartSdkPath = _systemDartSdkPath();
if (systemDartSdkPath == null) {
throw "Tried overriding Dart SDK of package '${sidekick.path}', but "
"couldn't get path of system Dart SDK.";
}
final dartSdk = sidekick.directory('build/cache/dart-sdk');
if (dartSdk.existsSync()) {
// otherwise Link.createSync throws an exception
dartSdk.deleteSync(recursive: true);
}
final dartSdkLink = Link(dartSdk.path)
..createSync(
systemDartSdkPath,
recursive: true,
);
print(
'Overrode Dart SDK at ${dartSdkLink.absolute.path} '
'to link to ${dartSdkLink.resolveSymbolicLinksSync()}',
);
}
void _overrideDependency({
required Directory package,
required String dependency,
required String path,
}) {
final pubspecPath = package.file('pubspec.yaml').path;
final pubspec = PubSpec.fromFile(pubspecPath);
pubspec.dependencyOverrides = {
...pubspec.dependencyOverrides,
dependency: Dependency.fromPath(dependency, path),
};
pubspec.save(pubspecPath);
}
/// Returns the Dart SDK of the `dart` executable on `PATH`
Directory? _systemDartSdk() {
// /opt/homebrew/bin/dart
final path = _systemDartExecutable();
if (path == null) {
// dart not on path
return null;
}
final file = File(path);
// /opt/homebrew/Cellar/dart/2.18.1/libexec/bin/dart
final realpath = file.resolveSymbolicLinksSync();
final libexec = File(realpath).parent.parent;
return libexec;
}
/// Returns the path to Dart SDK of the `dart` executable on `PATH`
String? _systemDartSdkPath() => _systemDartSdk()?.path;
String? _systemDartExecutable() =>
// /opt/homebrew/bin/dart
start('which dart', progress: Progress.capture(), nothrow: true).firstLine;