Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Current library's bib file is removed from the unlinked file search results #9755

Merged
merged 6 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed the double paste issue when <kbd>Cmd</kbd> + <kbd>v</kbd> is pressed on 'New entry from plaintext' dialog. [#9367](https://github.com/JabRef/jabref/issues/9367)
- We fixed an issue where the pin button on the Global Search dialog was located at the bottom and not at the top. [#9362](https://github.com/JabRef/jabref/issues/9362)
- We fixed the log text color in the event log console when using dark mode. [#9732](https://github.com/JabRef/jabref/issues/9732)
- We fixed an issue where searching for unlinked files would include the current library's .bib file [#9735](https://github.com/JabRef/jabref/issues/9735)

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public boolean accept(Path pathname) throws IOException {
if (Files.isDirectory(pathname)) {
return true;
} else {
return fileFilter.accept(pathname) && !lookup.lookupDatabase(pathname);
return fileFilter.accept(pathname) && !lookup.lookupDatabase(pathname) && !lookup.getPathOfDatabase().equals(pathname);
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/org/jabref/logic/util/io/DatabaseFileLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class DatabaseFileLookup {

private final List<Path> possibleFilePaths;

private final Path pathOfDatabase;

/**
* Creates an instance by passing a {@link BibDatabase} which will be used for the searches.
*/
Expand All @@ -37,6 +39,7 @@ public DatabaseFileLookup(BibDatabaseContext databaseContext, FilePreferences fi
for (BibEntry entry : databaseContext.getDatabase().getEntries()) {
fileCache.addAll(parseFileField(entry));
}
this.pathOfDatabase = databaseContext.getDatabasePath().orElse(Path.of(""));
}

/**
Expand Down Expand Up @@ -66,4 +69,11 @@ private List<Path> parseFileField(BibEntry entry) {
.map(Optional::get)
.collect(Collectors.toList());
}

/**
* @return "" if the path does not exist
*/
public Path getPathOfDatabase() {
return pathOfDatabase;
}
Comment on lines +76 to +78
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The most clean code would be to use Optional<Path> here. However, the only place using this would be more complex. @Siedlerchr Any oppinion here?

If this is kept as is, the JavaDoc comment should be put here

@returns "" if the Path does not exist

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep as is

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just wrong. A Path object can never be an empty string.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should return null then? Then, the calling code would be "clean". Think this method is never reached if the database is not saved.

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package org.jabref.gui.externalfiles;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;

import org.jabref.gui.util.FileNodeViewModel;
import org.jabref.logic.util.StandardFileType;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.preferences.FilePreferences;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static java.nio.file.DirectoryStream.Filter;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
Expand All @@ -35,4 +39,29 @@ public void minimalGitIgnore(@TempDir Path testRoot) throws Exception {

assertEquals(new FileNodeViewModel(testRoot), fileNodeViewModel);
}

@Test
public void excludingTheCurrentLibraryTest(@TempDir Path testRoot) throws IOException {
// Adding 3 files one of which is the database file
Files.createFile(testRoot.resolve("unlinkedPdf.pdf"));
Files.createFile(testRoot.resolve("another-unlinkedPdf.pdf"));
Path databasePath = testRoot.resolve("test.bib");
Files.createFile(databasePath);

BibDatabaseContext databaseContext = new BibDatabaseContext();
databaseContext.setDatabasePath(databasePath);

FilePreferences filePreferences = mock(FilePreferences.class);
Filter<Path> fileExtensionFilter = new FileExtensionViewModel(StandardFileType.ANY_FILE, filePreferences).dirFilter();
UnlinkedPDFFileFilter unlinkedPdfFileFilter = new UnlinkedPDFFileFilter(fileExtensionFilter, databaseContext, filePreferences);

UnlinkedFilesCrawler unlinkedFilesCrawler = new UnlinkedFilesCrawler(testRoot, unlinkedPdfFileFilter, DateRange.ALL_TIME, ExternalFileSorter.DEFAULT, databaseContext, filePreferences);
FileNodeViewModel fileNodeViewModel = unlinkedFilesCrawler.searchDirectory(testRoot, unlinkedPdfFileFilter);

// checking to see if the database file has been filtered
try (Stream<Path> filesInitially = Files.list(testRoot)) {
int count = (int) filesInitially.count();
assertEquals(fileNodeViewModel.getFileCount(), count - 1);
}
}
}