From a50ceeaa625cb105ef531cb37b04ce9afb99f37e Mon Sep 17 00:00:00 2001 From: jan Date: Sun, 24 Nov 2024 22:21:48 +0100 Subject: [PATCH] Add test on download and install lib on usage. Both test that when user does not wnat to download install Sloeber does not do it and the reverse. --- .../internal/ArduinoLibraryVersion.java | 7 ++-- .../src/io/sloeber/core/BuildTests.java | 40 ++++++++++++++++++ .../src/io/sloeber/core/Shared.java | 41 +++++++++++-------- .../onlyInstallLibraryWhenAllowed/sketch.ino | 14 +++++++ 4 files changed, 81 insertions(+), 21 deletions(-) create mode 100644 io.sloeber.tests/src/templates/onlyInstallLibraryWhenAllowed/sketch.ino diff --git a/io.sloeber.core/src/io/sloeber/arduinoFramework/internal/ArduinoLibraryVersion.java b/io.sloeber.core/src/io/sloeber/arduinoFramework/internal/ArduinoLibraryVersion.java index 0e4654c4..1d7ca727 100644 --- a/io.sloeber.core/src/io/sloeber/arduinoFramework/internal/ArduinoLibraryVersion.java +++ b/io.sloeber.core/src/io/sloeber/arduinoFramework/internal/ArduinoLibraryVersion.java @@ -67,7 +67,7 @@ public ArduinoLibraryVersion(JsonElement json, ArduinoLibrary arduinoLibrary) { archiveFileName = getSafeString(jsonObject, "archiveFileName"); size = jsonObject.get("size").getAsInt(); checksum = getSafeString(jsonObject, "checksum"); - calculateFQN(); + myFQN=calculateFQN(getName()); } catch (Exception e) { throw new JsonParseException("failed to parse json " + e.getMessage(),e); } @@ -205,9 +205,8 @@ public IPath getExamplePath() { return getInstallPath().append(EXAMPLES_FOLDER); } - private void calculateFQN() { - myFQN= Path.fromPortableString(SLOEBER_LIBRARY_FQN); - myFQN=myFQN.append(MANAGED).append(getName()); + static public IPath calculateFQN(String libName) { + return Path.fromPortableString(SLOEBER_LIBRARY_FQN).append(MANAGED).append(libName); } @Override diff --git a/io.sloeber.tests/src/io/sloeber/core/BuildTests.java b/io.sloeber.tests/src/io/sloeber/core/BuildTests.java index 962330d5..7ee82f81 100644 --- a/io.sloeber.tests/src/io/sloeber/core/BuildTests.java +++ b/io.sloeber.tests/src/io/sloeber/core/BuildTests.java @@ -31,6 +31,7 @@ import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; @@ -792,4 +793,43 @@ public void NightlyBoardPatron(String name, MCUBoard boardID, Example example, C } + @Test + public void onlyInstallLibraryWhenAllowed() throws Exception { + String libName="SD"; + + //set option not to install lib + ConfigurationPreferences.setInstallLibraries(false); + + //uninstall lib + LibraryManager.uninstallLibrary( libName); + + // create a project that uses a the lib + String testName = "onlyInstallLibraryWhenAllowed"; + IProgressMonitor monitor=new NullProgressMonitor(); + IPath templateFolder = Shared.getTemplateFolder(testName); + CodeDescription codeDescriptor = CodeDescription.createCustomTemplate(templateFolder); + MCUBoard unoboard = Arduino.uno(); + IProject theTestProject = SloeberProject.createArduinoProject(testName, null, unoboard.getBoardDescriptor(), codeDescriptor, + new CompileDescription(), monitor); + //wait for indexer and so on + Shared.waitForAllJobsToFinish(); + + + //Building the project should fail + assertNotNull( Shared.buildAndVerify(theTestProject,3,IncrementalProjectBuilder.FULL_BUILD ,monitor),"Sloeber wrongly installed lib "+libName); + + //set option to install libs + ConfigurationPreferences.setInstallLibraries(true); + + //trigger the indexer + ICProject cTestProject = CoreModel.getDefault().getCModel().getCProject(theTestProject.getName()); + CCorePlugin.getIndexManager().reindex(cTestProject); + Shared.waitForIndexer(theTestProject); + + //build should not fail + assertNull( Shared.buildAndVerify(theTestProject,3,IncrementalProjectBuilder.FULL_BUILD ,monitor),"Sloeber dit not install lib "+libName); + + + + } } diff --git a/io.sloeber.tests/src/io/sloeber/core/Shared.java b/io.sloeber.tests/src/io/sloeber/core/Shared.java index f65a7d00..bacfa795 100644 --- a/io.sloeber.tests/src/io/sloeber/core/Shared.java +++ b/io.sloeber.tests/src/io/sloeber/core/Shared.java @@ -31,6 +31,7 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform; @@ -259,23 +260,9 @@ public static String buildAndVerifyGivenBuilders(String projectName, BoardDescri autoDesc.setBuilder(curBuilder); coreModel.setProjectDescription(theTestProject, projectDescription); - theTestProject.build(IncrementalProjectBuilder.FULL_BUILD, monitor); - - if (hasBuildErrors(theTestProject)!=null) { - Shared.waitForAllJobsToFinish(); - Thread.sleep(2000); - theTestProject.build(IncrementalProjectBuilder.FULL_BUILD, monitor); - if (hasBuildErrors(theTestProject)!=null) { - Shared.waitForAllJobsToFinish(); - Thread.sleep(2000); - theTestProject.build(IncrementalProjectBuilder.FULL_BUILD, monitor); - String buildError=hasBuildErrors(theTestProject); - if (buildError!=null) { - myLastFailMessage = myLastFailMessage + NEWLINE +buildError+ NEWLINE+ "Failed to compile the project:" + projectName - + " with builder " + curBuilder; - } - } - } + buildAndVerify(theTestProject,3,IncrementalProjectBuilder.FULL_BUILD, monitor); + + } if (!myLastFailMessage.isBlank()) { @@ -295,6 +282,26 @@ public static String buildAndVerifyGivenBuilders(String projectName, BoardDescri return null; } + static String buildAndVerify(IProject theTestProject, int maxTries, int buildType, IProgressMonitor monitor) throws Exception { + int curTry = 0; + String buildError=null; + while (curTry++ < maxTries) { + theTestProject.build(buildType, monitor); + Shared.waitForAllJobsToFinish(); + Thread.sleep(2000); + buildError = hasBuildErrors(theTestProject); + if (buildError == null) { + return buildError; + } + } + IAutoBuildConfigurationDescription autoDesc = IAutoBuildConfigurationDescription + .getActiveConfig(theTestProject,false); + String builder=autoDesc.getBuilder().getId(); + myLastFailMessage = myLastFailMessage + NEWLINE + buildError + NEWLINE + "Failed to compile the project:" + + theTestProject.getName() + " with builder " + builder; + return buildError; + } + /* * For some boards that do not run out of the box we know how to fix it. This * code fixes these things diff --git a/io.sloeber.tests/src/templates/onlyInstallLibraryWhenAllowed/sketch.ino b/io.sloeber.tests/src/templates/onlyInstallLibraryWhenAllowed/sketch.ino new file mode 100644 index 00000000..69256a31 --- /dev/null +++ b/io.sloeber.tests/src/templates/onlyInstallLibraryWhenAllowed/sketch.ino @@ -0,0 +1,14 @@ +#include "Arduino.h" +#include "SD.h" +//The setup function is called once at startup of the sketch +void setup() +{ +// Add your initialization code here +} + +// The loop function is called in an endless loop +void loop() +{ +//Add your repeated code here +} +