Repository: SpoonLabs/sorald
Pull request #939 (2022-11-15 02:30:10)

fix(deps): update dependency fr.inria.gforge.spoon:spoon-core to v10.3.0-beta-3
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [fr.inria.gforge.spoon:spoon-core](http://spoon.gforge.inria.fr/) ([source](https://togithub.com/INRIA/spoon)) | `10.3.0-beta-2` -> `10.3.0-beta-3` | [![age](https://badges.renovateapi.com/packages/maven/fr.inria.gforge.spoon:spoon-core/10.3.0-beta-3/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/maven/fr.inria.gforge.spoon:spoon-core/10.3.0-beta-3/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/maven/fr.inria.gforge.spoon:spoon-core/10.3.0-beta-3/compatibility-slim/10.3.0-beta-2)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/maven/fr.inria.gforge.spoon:spoon-core/10.3.0-beta-3/confidence-slim/10.3.0-beta-2)](https://docs.renovatebot.com/merge-confidence/) |

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://app.renovatebot.com/dashboard#github/SpoonLabs/sorald).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC4yMy4xIiwidXBkYXRlZEluVmVyIjoiMzQuMjMuMSJ9-->



Patch:
diff --git a/pom.xml b/pom.xml
index e111635..9ef39f7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -79,7 +79,7 @@
         <dependency>
             <groupId>fr.inria.gforge.spoon</groupId>
             <artifactId>spoon-core</artifactId>
-            <version>10.3.0-beta-2</version>
+            <version>10.3.0-beta-3</version>
         </dependency>
         <dependency>
             <groupId>com.google.code.findbugs</groupId>
diff --git a/sorald/src/main/java/sorald/cli/MineCommand.java b/sorald/src/main/java/sorald/cli/MineCommand.java
index b37f51b..5dcff10 100644
--- a/sorald/src/main/java/sorald/cli/MineCommand.java
+++ b/sorald/src/main/java/sorald/cli/MineCommand.java
@@ -138,7 +138,7 @@ class MineCommand extends BaseCommand {
                             StatsMetadataKeys.EXECUTION_INFO,
                             new ExecutionInfo(
                                     originalArgs,
-                                    SoraldVersionProvider.getVersionFromPropertiesResource(
+                                    SoraldVersionProvider.getVersionFromManifests(
                                             SoraldVersionProvider.DEFAULT_RESOURCE_NAME),
                                     System.getProperty(Constants.JAVA_VERSION_SYSTEM_PROPERTY),
                                     target));
diff --git a/sorald/src/main/java/sorald/cli/RepairCommand.java b/sorald/src/main/java/sorald/cli/RepairCommand.java
index 9ba2239..aef0c2e 100644
--- a/sorald/src/main/java/sorald/cli/RepairCommand.java
+++ b/sorald/src/main/java/sorald/cli/RepairCommand.java
@@ -245,7 +245,7 @@ class RepairCommand extends BaseCommand {
         var executionInfo =
                 new ExecutionInfo(
                         originalArgs,
-                        SoraldVersionProvider.getVersionFromPropertiesResource(
+                        SoraldVersionProvider.getVersionFromManifests(
                                 SoraldVersionProvider.DEFAULT_RESOURCE_NAME),
                         System.getProperty(Constants.JAVA_VERSION_SYSTEM_PROPERTY),
                         target);
diff --git a/sorald/src/main/java/sorald/cli/SoraldVersionProvider.java b/sorald/src/main/java/sorald/cli/SoraldVersionProvider.java
index 50917ca..63d924b 100644
--- a/sorald/src/main/java/sorald/cli/SoraldVersionProvider.java
+++ b/sorald/src/main/java/sorald/cli/SoraldVersionProvider.java
@@ -1,20 +1,27 @@
 package sorald.cli;
 
-import java.util.Properties;
+import java.io.IOException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.jar.Attributes;
+import java.util.jar.JarException;
+import java.util.jar.Manifest;
 import picocli.CommandLine;
 
 /** Class for providing the CLI with the current version. */
 public class SoraldVersionProvider implements CommandLine.IVersionProvider {
     public static final String LOCAL_VERSION = "LOCAL";
 
+    static final String MAIN_CLASS = "Main-Class";
+    static final String MAIN_CLASS_NAME = "sorald.Main";
     static final String VERSION_KEY = "Implementation-Version";
     static final String COMMIT_KEY = "Implementation-SCM-Revision";
 
     public static final String DEFAULT_RESOURCE_NAME = "META-INF/MANIFEST.MF";
 
     @Override
-    public String[] getVersion() {
-        return new String[] {getVersionFromPropertiesResource(DEFAULT_RESOURCE_NAME)};
+    public String[] getVersion() throws JarException {
+        return new String[] {getVersionFromManifests(DEFAULT_RESOURCE_NAME)};
     }
 
     /**
@@ -24,24 +31,38 @@ public class SoraldVersionProvider implements CommandLine.IVersionProvider {
      * @param resourceName Name of the resource.
      * @return The resolved version.
      */
-    public static String getVersionFromPropertiesResource(String resourceName) {
-        Properties props = new Properties();
+    public static String getVersionFromManifests(String resourceName) throws JarException {
         try {
-            props.load(SoraldVersionProvider.class.getResourceAsStream("/" + resourceName));
-        } catch (Exception e) {
+            Enumeration<URL> resources =
+                    Thread.currentThread().getContextClassLoader().getResources(resourceName);
+            while (resources.hasMoreElements()) {
+                Manifest jarManifest = new Manifest(resources.nextElement().openStream());
+                String mainClass = jarManifest.getMainAttributes().getValue(MAIN_CLASS);
+                if (mainClass != null && mainClass.equals(MAIN_CLASS_NAME)) {
+                    return getVersionFromSoraldManifest(jarManifest);
+                }
+            }
+        } catch (JarException e) {
+            throw new JarException(e.getMessage());
+        } catch (IOException e) {
             return LOCAL_VERSION;
         }
-        return getVersionFromProperties(props);
+        return LOCAL_VERSION;
     }
 
-    private static String getVersionFromProperties(Properties props) {
-        String version = props.getProperty(VERSION_KEY);
-        String commitSha = props.getProperty(COMMIT_KEY);
+    private static String getVersionFromSoraldManifest(Manifest manifest) throws JarException {
+        Attributes mainAttributes = manifest.getMainAttributes();
+        String version = mainAttributes.getValue(VERSION_KEY);
+        String commitSha = mainAttributes.getValue(COMMIT_KEY);
 
-        if (version == null || commitSha == null) {
-            return LOCAL_VERSION;
-        } else {
-            return version.contains("SNAPSHOT") ? "commit: " + commitSha : version;
+        if (version == null) {
+            throw new JarException(String.format("%s not set in POM.", VERSION_KEY));
+        }
+
+        if (commitSha == null) {
+            throw new JarException(String.format("%s not set in POM.", COMMIT_KEY));
         }
+
+        return version.contains("SNAPSHOT") ? "commit: " + commitSha : version;
     }
 }


Test patch:
diff --git a/sorald/src/test/java/sorald/cli/SoraldVersionProviderTest.java b/sorald/src/test/java/sorald/cli/SoraldVersionProviderTest.java
index 074cb5b..06054d9 100644
--- a/sorald/src/test/java/sorald/cli/SoraldVersionProviderTest.java
+++ b/sorald/src/test/java/sorald/cli/SoraldVersionProviderTest.java
@@ -2,46 +2,56 @@ package sorald.cli;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.equalTo;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.jar.JarException;
 import org.junit.jupiter.api.Test;
 
-public class SoraldVersionProviderTest {
+class SoraldVersionProviderTest {
     private static final Path BOGUS_MANIFESTS = Paths.get("META-INF").resolve("bogus-manifests");
 
     private static final String VERSION_IN_MANIFESTS = "1.2.3";
     private static final String COMMIT_IN_MANIFESTS = "123456";
 
     @Test
-    public void getVersionFromPropertiesResource_returnsLocalVersion_whenResourceDoesNotExist() {
+    void getVersionFromPropertiesResource_returnsLocalVersion_whenResourceDoesNotExist()
+            throws JarException {
         assertThat(
-                SoraldVersionProvider.getVersionFromPropertiesResource("no/such/resource"),
+                SoraldVersionProvider.getVersionFromManifests("no/such/resource"),
                 equalTo(SoraldVersionProvider.LOCAL_VERSION));
     }
 
     @Test
-    public void getVersionFromPropertiesResource_returnsVersion_whenNonSnapshot() {
+    void getVersionFromPropertiesResource_returnsVersion_whenNonSnapshot() throws JarException {
         String resourceName = BOGUS_MANIFESTS.resolve("MANIFEST-RELEASE-VERSION.MF").toString();
         assertThat(
-                SoraldVersionProvider.getVersionFromPropertiesResource(resourceName),
+                SoraldVersionProvider.getVersionFromManifests(resourceName),
                 equalTo(VERSION_IN_MANIFESTS));
     }
 
     @Test
-    public void getVersionFromPropertiesResource_returnsCommitSha_whenSnapshot() {
+    void getVersionFromPropertiesResource_returnsCommitSha_whenSnapshot() throws JarException {
         String resourceName = BOGUS_MANIFESTS.resolve("MANIFEST-SNAPSHOT-VERSION.MF").toString();
         assertThat(
-                SoraldVersionProvider.getVersionFromPropertiesResource(resourceName),
+                SoraldVersionProvider.getVersionFromManifests(resourceName),
                 equalTo("commit: " + COMMIT_IN_MANIFESTS));
     }
 
     @Test
-    public void
-            getVersionFromPropertiesResource_returnsLocalVersion_whenResourceIsMissingVersion() {
+    void getVersionFromPropertiesResource_throwsJarException_whenResourceIsMissingVersion() {
         String resourceName = BOGUS_MANIFESTS.resolve("MANIFEST-WITHOUT-VERSION.MF").toString();
-        assertThat(
-                SoraldVersionProvider.getVersionFromPropertiesResource(resourceName),
-                equalTo(SoraldVersionProvider.LOCAL_VERSION));
+        assertThrows(
+                JarException.class,
+                () -> SoraldVersionProvider.getVersionFromManifests(resourceName));
+    }
+
+    @Test
+    void getVersionFromPropertiesResource_throwsJarException_whenResourceIsMissingCommitSha() {
+        String resourceName = BOGUS_MANIFESTS.resolve("MANIFEST-WITHOUT-COMMIT-SHA.MF").toString();
+        assertThrows(
+                JarException.class,
+                () -> SoraldVersionProvider.getVersionFromManifests(resourceName));
     }
 }
diff --git a/sorald/src/test/java/sorald/miner/WarningMinerTest.java b/sorald/src/test/java/sorald/miner/WarningMinerTest.java
index 6138571..6054255 100644
--- a/sorald/src/test/java/sorald/miner/WarningMinerTest.java
+++ b/sorald/src/test/java/sorald/miner/WarningMinerTest.java
@@ -170,7 +170,7 @@ public class WarningMinerTest {
         assertThat(
                 executionInfo.get(StatsMetadataKeys.SORALD_VERSION),
                 equalTo(
-                        SoraldVersionProvider.getVersionFromPropertiesResource(
+                        SoraldVersionProvider.getVersionFromManifests(
                                 SoraldVersionProvider.DEFAULT_RESOURCE_NAME)));
         assertThat(
                 executionInfo.get(StatsMetadataKeys.JAVA_VERSION),
diff --git a/sorald/src/test/resources/META-INF/bogus-manifests/MANIFEST-RELEASE-VERSION.MF b/sorald/src/test/resources/META-INF/bogus-manifests/MANIFEST-RELEASE-VERSION.MF
index 987d225..a107e5d 100644
--- a/sorald/src/test/resources/META-INF/bogus-manifests/MANIFEST-RELEASE-VERSION.MF
+++ b/sorald/src/test/resources/META-INF/bogus-manifests/MANIFEST-RELEASE-VERSION.MF
@@ -1,3 +1,4 @@
 Manifest-Version: 1.0
 Implementation-Version: 1.2.3
 Implementation-SCM-Revision: 123456
+Main-Class: sorald.Main
diff --git a/sorald/src/test/resources/META-INF/bogus-manifests/MANIFEST-SNAPSHOT-VERSION.MF b/sorald/src/test/resources/META-INF/bogus-manifests/MANIFEST-SNAPSHOT-VERSION.MF
index d3d4d87..a5b90e3 100644
--- a/sorald/src/test/resources/META-INF/bogus-manifests/MANIFEST-SNAPSHOT-VERSION.MF
+++ b/sorald/src/test/resources/META-INF/bogus-manifests/MANIFEST-SNAPSHOT-VERSION.MF
@@ -1,3 +1,4 @@
 Manifest-Version: 1.0
 Implementation-Version: 1.2.3-SNAPSHOT
 Implementation-SCM-Revision: 123456
+Main-Class: sorald.Main
diff --git a/sorald/src/test/resources/META-INF/bogus-manifests/MANIFEST-WITHOUT-COMMIT-SHA.MF b/sorald/src/test/resources/META-INF/bogus-manifests/MANIFEST-WITHOUT-COMMIT-SHA.MF
new file mode 100644
index 0000000..3eee75d
--- /dev/null
+++ b/sorald/src/test/resources/META-INF/bogus-manifests/MANIFEST-WITHOUT-COMMIT-SHA.MF
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Implementation-Version: 1.2.3-SNAPSHOT
+Main-Class: sorald.Main
diff --git a/sorald/src/test/resources/META-INF/bogus-manifests/MANIFEST-WITHOUT-VERSION.MF b/sorald/src/test/resources/META-INF/bogus-manifests/MANIFEST-WITHOUT-VERSION.MF
index 587ebc8..07b1b6b 100644
--- a/sorald/src/test/resources/META-INF/bogus-manifests/MANIFEST-WITHOUT-VERSION.MF
+++ b/sorald/src/test/resources/META-INF/bogus-manifests/MANIFEST-WITHOUT-VERSION.MF
@@ -1,2 +1,3 @@
 Manifest-Version: 1.0
 Implementation-SCM-Revision: 123456
+Main-Class: sorald.Main