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

Follow up the "Aux file" on "Edit group" doesn't support relative sub-directories path #7950

Merged
merged 12 commits into from
Aug 2, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
### Fixed

- We fixed an issue when checking for a new version when JabRef is used behind a corporate proxy. [#7884](https://github.com/JabRef/jabref/issues/7884)
- We fixed an issue where the `Aux file` on `Edit group` doesn't support relative sub-directories path to import. [#7719](https://github.com/JabRef/jabref/issues/7719).
- We fixed an issue where it was impossible to add or modify groups. [#7912](https://github.com/JabRef/jabref/pull/793://github.com/JabRef/jabref/pull/7921)
- We fixed an issue where exported entries from a Citavi bib containing URLs could not be imported [#7892](https://github.com/JabRef/jabref/issues/7882)

Expand Down
14 changes: 12 additions & 2 deletions src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ private void setupValidation() {
if (StringUtil.isBlank(input)) {
return false;
} else {
Path texFilePath = preferencesService.getWorkingDir().resolve(input);
if (!Files.isRegularFile(texFilePath)) {
Path inputPath = getAbsoluteTexGroupPath(input);
if (!inputPath.isAbsolute() || !Files.isRegularFile(inputPath)) {
return false;
}
return FileUtil.getFileExtension(input)
Expand Down Expand Up @@ -267,6 +267,16 @@ private void setupValidation() {
});
}

/**
* Gets the absolute path relative to the LatexFileDirectory, if given a relative path
* @param input the user input path
* @return an absolute path if LatexFileDirectory exists; otherwise, returns input
*/
private Path getAbsoluteTexGroupPath(String input) {
Optional<Path> latexFileDirectory = currentDatabase.getMetaData().getLatexFileDirectory(preferencesService.getUser());
return latexFileDirectory.map(path -> path.resolve(input)).orElse(Path.of(input));
}

public void validationHandler(Event event) {
ValidationStatus validationStatus = validator.getValidationStatus();
if (validationStatus.getHighestMessage().isPresent()) {
Expand Down
77 changes: 77 additions & 0 deletions src/test/java/org/jabref/gui/groups/GroupDialogViewModelTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.jabref.gui.groups;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;

import org.jabref.gui.DialogService;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.groups.AbstractGroup;
import org.jabref.model.metadata.MetaData;
import org.jabref.preferences.PreferencesService;

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

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class GroupDialogViewModelTest {

private GroupDialogViewModel viewModel;
private Path temporaryFolder;
private BibDatabaseContext bibDatabaseContext;
private final MetaData metaData = mock(MetaData.class);

@BeforeEach
void setUp(@TempDir Path temporaryFolder) throws Exception {
this.temporaryFolder = temporaryFolder;
bibDatabaseContext = new BibDatabaseContext();
DialogService dialogService = mock(DialogService.class);

AbstractGroup group = mock(AbstractGroup.class);
when(group.getName()).thenReturn("Group");

PreferencesService preferencesService = mock(PreferencesService.class);
when(preferencesService.getKeywordDelimiter()).thenReturn(',');
when(preferencesService.getUser()).thenReturn("MockedUser");

bibDatabaseContext.setMetaData(metaData);

viewModel = new GroupDialogViewModel(dialogService, bibDatabaseContext, preferencesService, group, GroupDialogHeader.SUBGROUP);
}

@Test
void validateExistingAbsolutePath() throws Exception {
var anAuxFile = temporaryFolder.resolve("auxfile.aux").toAbsolutePath();

Files.createFile(anAuxFile);
when(metaData.getLatexFileDirectory(any(String.class))).thenReturn(Optional.of(temporaryFolder));

viewModel.texGroupFilePathProperty().setValue(anAuxFile.toString());
assertTrue(viewModel.texGroupFilePathValidatonStatus().isValid());
}

@Test
void validateNonExistingAbsolutePath() throws Exception {
var notAnAuxFile = temporaryFolder.resolve("notanauxfile.aux").toAbsolutePath();
viewModel.texGroupFilePathProperty().setValue(notAnAuxFile.toString());
assertFalse(viewModel.texGroupFilePathValidatonStatus().isValid());
}

@Test
void validateExistingRelativePath() throws Exception {
var anAuxFile = Path.of("auxfile.aux");

// The file needs to exist
Files.createFile(temporaryFolder.resolve(anAuxFile));
when(metaData.getLatexFileDirectory(any(String.class))).thenReturn(Optional.of(temporaryFolder));

viewModel.texGroupFilePathProperty().setValue(anAuxFile.toString());
assertTrue(viewModel.texGroupFilePathValidatonStatus().isValid());
}
}