Skip to content

Commit

Permalink
Follow up the "Aux file" on "Edit group" doesn't support relative sub…
Browse files Browse the repository at this point in the history
…-directories path (#7950)

Co-authored-by: Jonatan Asketorp <2598631+k3KAW8Pnf7mkmdSMPHz27@users.noreply.github.com>
Co-authored-by: aloofluo <945517787@qq.com>
  • Loading branch information
3 people authored Aug 2, 2021
1 parent 7be46bb commit f713d94
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
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());
}
}

0 comments on commit f713d94

Please sign in to comment.