Skip to content

Commit c4fc97b

Browse files
author
jan
committed
Add test for #1723
1 parent 36b35e8 commit c4fc97b

File tree

1 file changed

+98
-1
lines changed

1 file changed

+98
-1
lines changed

io.sloeber.tests/src/io/sloeber/core/BuildTests.java

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44

5-
5+
import static io.sloeber.core.api.Const.*;
66
import java.io.File;
77
import java.util.Arrays;
88
import java.util.Collection;
@@ -15,6 +15,8 @@
1515
import java.util.TreeMap;
1616
import java.util.stream.Stream;
1717
import java.net.URI;
18+
import java.nio.file.Files;
19+
import java.nio.file.StandardOpenOption;
1820

1921
import org.apache.commons.io.FileUtils;
2022

@@ -25,8 +27,10 @@
2527
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
2628
import org.eclipse.cdt.internal.core.pdom.indexer.IndexerPreferences;
2729
import org.eclipse.core.resources.IFile;
30+
import org.eclipse.core.resources.IFolder;
2831
import org.eclipse.core.resources.IProject;
2932
import org.eclipse.core.resources.IWorkspace;
33+
import org.eclipse.core.resources.IWorkspaceRoot;
3034
import org.eclipse.core.resources.IncrementalProjectBuilder;
3135
import org.eclipse.core.resources.ResourcesPlugin;
3236
import org.eclipse.core.runtime.CoreException;
@@ -801,4 +805,97 @@ public void NightlyBoardPatron(String name, MCUBoard boardID, Example example, C
801805

802806
}
803807

808+
809+
/**
810+
* Use private lib from the workspace.
811+
* Close project
812+
* open project
813+
* Is the lib still there (as does project still build)
814+
*
815+
* @throws Exception
816+
*/
817+
@Test
818+
public void issue1723() throws Exception {
819+
final String projectName = "private_lib";
820+
final String privateLibFolderName = "an_private_lib";
821+
final String privateLibName = "a_private_lib";
822+
final String libHeaderContent=("int aFunction();")+System.lineSeparator();
823+
String libCodeContent=("#include \""+privateLibName+".h\"")+System.lineSeparator();
824+
libCodeContent=libCodeContent+("int aFunction(){")+System.lineSeparator();
825+
libCodeContent=libCodeContent+("}")+System.lineSeparator();
826+
String libRefContent=("#include \""+privateLibName+".h\"")+System.lineSeparator();
827+
libRefContent=libRefContent+("int aRefFunction(){")+System.lineSeparator();
828+
libRefContent=libRefContent+("aFunction();")+System.lineSeparator();
829+
libRefContent=libRefContent+("}")+System.lineSeparator();
830+
831+
832+
//create a basic arduino project
833+
BoardDescription unoBoardid = Arduino.uno().getBoardDescriptor();
834+
IProject theTestProject = null;
835+
836+
IPath templateFolder = Shared.getTemplateFolder("CreateAndCompileTest");
837+
CodeDescription codeDescriptor = CodeDescription.createCustomTemplate(templateFolder);
838+
theTestProject = SloeberProject.createArduinoProject(projectName, null, unoBoardid, codeDescriptor,
839+
new CompileDescription(), new NullProgressMonitor());
840+
Shared.waitForIndexer(theTestProject);
841+
842+
//create a private library project
843+
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
844+
IWorkspaceRoot root = workspace.getRoot();
845+
IProject theLibProject= root.getProject("PrivateLibs");
846+
theLibProject.create(new NullProgressMonitor());
847+
theLibProject.open(new NullProgressMonitor());
848+
IFolder libFolder = theLibProject.getFolder(privateLibFolderName);
849+
libFolder.create(true, true, new NullProgressMonitor());
850+
IFile libHeaderFile=libFolder.getFile(privateLibName+".h");
851+
IFile libSourceFile=libFolder.getFile(privateLibName+".cpp");
852+
853+
Files.write(libHeaderFile.getLocation().toPath(), libHeaderContent.getBytes(), StandardOpenOption.TRUNCATE_EXISTING,
854+
StandardOpenOption.CREATE);
855+
Files.write(libSourceFile.getLocation().toPath(), libCodeContent.getBytes(), StandardOpenOption.TRUNCATE_EXISTING,
856+
StandardOpenOption.CREATE);
857+
858+
859+
//build project (should work)
860+
theTestProject.build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor());
861+
assertNull(Shared.hasBuildErrors(theTestProject),"Created Project does not build.");
862+
863+
864+
//Add code to project that uses private lib
865+
IFolder srcFolder = theTestProject.getFolder("src");
866+
IFile referingFile=srcFolder.getFile("privateLibUser.cpp");
867+
Files.write(referingFile.getLocation().toPath(), libRefContent.getBytes(), StandardOpenOption.TRUNCATE_EXISTING,
868+
StandardOpenOption.CREATE);
869+
870+
//build project (should fail)
871+
theTestProject.build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor());
872+
assertNotNull(Shared.hasBuildErrors(theTestProject),"Lib should be missing; build should fail.");
873+
874+
875+
//add private libs project to the sloeber preferences private libs
876+
List<String> privateLibList= new LinkedList<>();
877+
privateLibList.add(theLibProject.getLocation().toOSString());
878+
privateLibList.addAll( Arrays.asList( LibraryManager.getPrivateLibraryPaths()));
879+
LibraryManager.setPrivateLibraryPaths(privateLibList.toArray(new String[privateLibList.size()]));
880+
881+
882+
883+
//add the private lib to the project
884+
IArduinoLibraryVersion privateArduinoLib=LibraryManager.getLibraryVersionFromFQN(SLOEBER_LIBRARY_FQN+SLACH+PRIVATE+SLACH+libFolder.getName(), null);
885+
Collection<IArduinoLibraryVersion> myPrivateLibs =new LinkedList<>();
886+
myPrivateLibs.add(privateArduinoLib);
887+
888+
ISloeberConfiguration sloeberConf=ISloeberConfiguration.getActiveConfig(theTestProject, true);
889+
sloeberConf.addLibraries(myPrivateLibs);
890+
891+
//build project (should work)
892+
theTestProject.build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor());
893+
assertNull(Shared.hasBuildErrors(theTestProject),"lib added build should succeed");
894+
895+
//There should be 1 lib in the project
896+
Map<IPath, IArduinoLibraryVersion> usedLibs=sloeberConf.getUsedLibraries();
897+
assertTrue(usedLibs.size()==0,"Private Lib not found");
898+
899+
900+
}
804901
}

0 commit comments

Comments
 (0)