Handle NullPointerException in hgrc clean-up

We encountered NullPointerExceptions during hg repository imports. This happened, when neither authentication nor proxy definions had been given. In this case, no hgrc will be created and therefore we get the exception in the clean-up step. Now, the clean-up is only triggered, if the hgrc had been created beforehand.

To prevent further error in the clean-up, we no catch other potential exceptions and log them but do not let them break the import.

Pushed-by: Rene Pfeuffer<rene.pfeuffer@cloudogu.com>
Co-authored-by: René Pfeuffer<rene.pfeuffer@cloudogu.com>
This commit is contained in:
Rene Pfeuffer
2024-08-29 08:50:51 +02:00
parent 316d03ad1a
commit a2d82372e5
3 changed files with 16 additions and 1 deletions

View File

@@ -0,0 +1,2 @@
- type: fixed
description: Import of hg repositories without credentials

View File

@@ -94,7 +94,13 @@ public class TemporaryConfigFactory {
if (!exists && file.exists() && !file.delete()) {
LOG.error("failed to delete temporary hgrc {}", file);
} else if (exists && file.exists()) {
cleanUpHgrc(file);
try {
if (hgrc != null) {
cleanUpHgrc(file);
}
} catch (Exception e) {
LOG.warn("error cleaning up hgrc", e);
}
}
}
}

View File

@@ -73,6 +73,13 @@ class TemporaryConfigFactoryTest {
});
}
@Test
@SuppressWarnings("java:S2699") // test should just ensure that no exception is thrown
void shouldNotFailIfFileExistsButWithoutProxyOrAuthentication() throws IOException {
Files.createFile(hgrc);
configFactory.withContext(commandContext).call(() -> null);
}
@Test
void shouldCreateHgrcWithAuthentication() throws IOException {
configFactory