|
2 | 2 |
|
3 | 3 |
|
4 | 4 |
|
5 |
| - |
| 5 | +import static io.sloeber.core.api.Const.*; |
6 | 6 | import java.io.File;
|
7 | 7 | import java.util.Arrays;
|
8 | 8 | import java.util.Collection;
|
|
15 | 15 | import java.util.TreeMap;
|
16 | 16 | import java.util.stream.Stream;
|
17 | 17 | import java.net.URI;
|
| 18 | +import java.nio.file.Files; |
| 19 | +import java.nio.file.StandardOpenOption; |
18 | 20 |
|
19 | 21 | import org.apache.commons.io.FileUtils;
|
20 | 22 |
|
|
25 | 27 | import org.eclipse.cdt.core.settings.model.ICProjectDescription;
|
26 | 28 | import org.eclipse.cdt.internal.core.pdom.indexer.IndexerPreferences;
|
27 | 29 | import org.eclipse.core.resources.IFile;
|
| 30 | +import org.eclipse.core.resources.IFolder; |
28 | 31 | import org.eclipse.core.resources.IProject;
|
29 | 32 | import org.eclipse.core.resources.IWorkspace;
|
| 33 | +import org.eclipse.core.resources.IWorkspaceRoot; |
30 | 34 | import org.eclipse.core.resources.IncrementalProjectBuilder;
|
31 | 35 | import org.eclipse.core.resources.ResourcesPlugin;
|
32 | 36 | import org.eclipse.core.runtime.CoreException;
|
@@ -801,4 +805,97 @@ public void NightlyBoardPatron(String name, MCUBoard boardID, Example example, C
|
801 | 805 |
|
802 | 806 | }
|
803 | 807 |
|
| 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 | + } |
804 | 901 | }
|
0 commit comments