Skip to content

Commit

Permalink
Fix getter for kradle cache which should create the dir instead of th…
Browse files Browse the repository at this point in the history
…rowing.
  • Loading branch information
buijs-dev committed Apr 20, 2024
1 parent 4fe3263 commit e5a9227
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
16 changes: 7 additions & 9 deletions lib/src/common/project.dart
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ extension on Directory {
userHome.error,
);

return _kradleCacheDirectoryOrThrow(
return _kradleCacheDirectory(
kradleHome, () => throw KlutterException(errorMessage));
}
}
Expand All @@ -382,7 +382,7 @@ extension on File {
/// is not resolved or does not exist.
Directory get defaultKradleCache {
final userHome = _userHomeOrError;
return _kradleCacheDirectoryOrThrow(
return _kradleCacheDirectory(
_kradleCacheFromEnvironmentPropertyOrNull(userHome.userHome), () {
throw KlutterException(
_defaultKradleCacheBecauseCachePropertyNotFoundErrorMessage(
Expand All @@ -404,7 +404,7 @@ extension on File {
kradleEnvPropertyUserHome, userHome.userHome!)
: null;

return _kradleCacheDirectoryOrThrow(
return _kradleCacheDirectory(
cachePropertyResolved,
() => throw KlutterException(
_configuredKradleHomeErrorMessage(
Expand All @@ -415,7 +415,7 @@ extension on File {
);
}

return _kradleCacheDirectoryOrThrow(cacheProperty, () {});
return _kradleCacheDirectory(cacheProperty, () {});
}

/// Return value of property cache or null.
Expand All @@ -434,18 +434,16 @@ String _extractPropertyValue(String line) =>
String? _kradleCacheFromEnvironmentPropertyOrNull(String? userHomeOrNull) =>
userHomeOrNull == null ? null : "$userHomeOrNull/.kradle/cache".normalize;

/// Returns the kradle cache directory or throws a [KlutterException]
/// if the directory could not be determined or if it does not exist.
Directory _kradleCacheDirectoryOrThrow(
/// Returns the kradle cache directory and creates it if it does not exist.
Directory _kradleCacheDirectory(
String? pathToKradleCache,
void Function() onNullValue,
) {
if (pathToKradleCache == null) {
onNullValue();
}

return Directory(pathToKradleCache!.normalize).normalizeToFolder
..verifyFolderExists;
return Directory(pathToKradleCache!.normalize).normalizeToFolder..maybeCreate;
}

/// Determine the user home directory by checking environment variables.
Expand Down
17 changes: 12 additions & 5 deletions test/src/common/project_kradle_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,25 @@ void main() {
});

test(
"When kradle.env contains cache property and the file is not found then an exception is thrown",
"When kradle.env contains cache property and the directory is not found then it is created",
() {
final dotKradleDirectory = Directory.systemTemp.resolveFolder(".kradle")..createSync();
final cacheDirectory = Directory("${dotKradleDirectory.absolutePath}/cache".normalize);
final rootDirectory = _newProjectFolder;
setPlatformMacos(rootDirectory);
createCacheFolder(rootDirectory);
createKradleEnv(
rootDirectory,
contents: "cache=foo/bar/.kradle/cache",
contents: "cache=${cacheDirectory.absolutePath}",
);
expect(() => rootDirectory.kradleCache, throwsA(predicate((e) {
return e is KlutterException && e.cause.contains("Path does not exist: ");
})));

expect(cacheDirectory.existsSync(), false);

// when
rootDirectory.kradleCache;

// then
expect(cacheDirectory.existsSync(), true);
});

tearDownAll(() {
Expand Down
5 changes: 4 additions & 1 deletion test/src/systemtest/e2e_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,10 @@ Future<void> createFlutterPlugin({

for(final task in tasks) {
final res = await task.execute(root);
//assert(res.isOk, res.message);
if(!res.isOk) {
print(task);
assert(res.isOk,res.message);
}
}

}

0 comments on commit e5a9227

Please sign in to comment.