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

Fix reported pathing issues when using Import #311

Merged
merged 7 commits into from
Nov 11, 2024
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
37 changes: 26 additions & 11 deletions src/main/java/seedu/address/logic/commands/ImportCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand Down Expand Up @@ -132,24 +133,38 @@ public CommandResult execute(Model model) throws CommandException {
}

/**
* Resolves the file path, handling both home directory paths (starting with ~) and parent directory paths.
* Resolves the file path, handling different possible locations in priority order:
* 1. Data directory (./data/)
* 2. Current directory
* 3. Home directory paths (starting with ~)
* 4. Absolute paths
*/
protected Path resolveFilePath(String filepath) {
// Remove .csv extension if present for consistency
String filename = filepath.endsWith(".csv")
? filepath.substring(0, filepath.length() - 4)
: filepath;

// Check data directory first
Path dataPath = Paths.get("data", filename + ".csv");
if (Files.exists(dataPath)) {
return dataPath;
}

// Then try the direct path in case it's in current directory
Path directPath = Paths.get(filepath);
if (Files.exists(directPath)) {
return directPath;
}

// If path starts with ~, expand to user home directory
if (filepath.startsWith("~")) {
return Paths.get(System.getProperty("user.home"))
.resolve(filepath.substring(2)); // Remove ~/ from path
}

// For relative paths, check parent directory of project
if (!Paths.get(filepath).isAbsolute()) {
return Paths.get(System.getProperty("user.dir"))
.getParent() // Go up one level from project directory
.resolve(filepath);
.resolve(filepath.substring(2));
}

// For absolute paths, use as is
return Paths.get(filepath);
// If nothing found, default to data directory path for error message consistency
return dataPath;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand Down Expand Up @@ -142,21 +144,49 @@
}

/**
* Resolves the file path, handling both home directory paths (starting with ~) and parent directory paths.
* Resolves the file path, handling different possible locations in priority order:
* 1. Data directory (./data/)
* 2. Current directory
* 3. Home directory paths (starting with ~)
* 4. Absolute paths
*/
protected Path resolveFilePath(String filepath) {
if (filepath.startsWith("~")) {
return Paths.get(System.getProperty("user.home"))
.resolve(filepath.substring(2));
}
try {
// Remove .csv extension if present for consistency
String filename = filepath.endsWith(".csv")
? filepath.substring(0, filepath.length() - 4)
: filepath;

// Check data directory first
Path dataPath = Paths.get("data").resolve(filename + ".csv").normalize();
if (Files.exists(dataPath)) {
return dataPath;
}

if (!Paths.get(filepath).isAbsolute()) {
return Paths.get(System.getProperty("user.dir"))
.getParent()
.resolve(filepath);
}
// Then try the direct path in case it's in current directory
Path directPath = Paths.get(filename + ".csv").normalize();
if (Files.exists(directPath)) {
return directPath;
}

return Paths.get(filepath);
// If path starts with ~, expand to user home directory
if (filename.startsWith("~")) {
return Paths.get(System.getProperty("user.home"))
.resolve(filename.substring(2) + ".csv")
.normalize();
}

// For absolute paths, try to handle them directly
if (Paths.get(filepath).isAbsolute()) {
return Paths.get(filepath).normalize();
}

// If nothing found, default to data directory path for error message consistency
return dataPath;
} catch (InvalidPathException e) {

Check warning on line 186 in src/main/java/seedu/address/logic/commands/consultation/ImportConsultCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/consultation/ImportConsultCommand.java#L186

Added line #L186 was not covered by tests
// If we get an invalid path, fall back to treating it as a simple filename in data directory
return Paths.get("data", filepath).normalize();

Check warning on line 188 in src/main/java/seedu/address/logic/commands/consultation/ImportConsultCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/consultation/ImportConsultCommand.java#L188

Added line #L188 was not covered by tests
}
}

private Path writeErrorFile(List<String[]> errorEntries) throws IOException {
Expand Down
55 changes: 47 additions & 8 deletions src/test/java/seedu/address/logic/commands/ImportCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,16 +309,23 @@ protected Path resolveFilePath(String filepath) {

@Test
public void execute_absolutePath() throws IOException, CommandException {
// Create file with absolute path
Path absolutePath = testDir.toAbsolutePath().resolve("absolute.csv");
try (FileWriter writer = new FileWriter(absolutePath.toFile())) {
writer.write("Name,Phone,Email,Courses\n");
writer.write("John Doe,12345678,john@example.com,CS2103T\n");
}
// Create file using platform-independent path handling
Path absolutePath = testDir.toAbsolutePath().normalize().resolve("absolute.csv");
Files.createDirectories(absolutePath.getParent());

Files.writeString(absolutePath,
"Name,Phone,Email,Courses\n"
+ "John Doe,12345678,john@example.com,CS2103T\n"
);
filesToCleanup.add(absolutePath);

ImportCommand importCommand = new ImportCommand(absolutePath.toString());
CommandResult result = importCommand.execute(model);
ImportCommand testCommand = new ImportCommand(absolutePath.toString()) {
@Override
protected Path resolveFilePath(String filepath) {
return Paths.get(filepath).normalize();
}
};
CommandResult result = testCommand.execute(model);
assertEquals(String.format(ImportCommand.MESSAGE_SUCCESS, 1, 0), result.getFeedbackToUser());
}

Expand Down Expand Up @@ -424,4 +431,36 @@ protected Path resolveFilePath(String filepath) {
CommandResult result = importCommand.execute(model);
assertEquals(String.format(ImportCommand.MESSAGE_SUCCESS, 1, 0), result.getFeedbackToUser());
}

@Test
public void execute_dataDirectoryPath_success() throws IOException, CommandException {
// Create test file in data directory
Path dataDir = Paths.get("data");
Files.createDirectories(dataDir);
Path testFile = dataDir.resolve("students.csv");

try (FileWriter writer = new FileWriter(testFile.toFile())) {
writer.write("Name,Phone,Email,Courses\n");
writer.write("John Doe,12345678,john@example.com,CS2103T\n");
}
filesToCleanup.add(testFile);

ImportCommand importCommand = new ImportCommand("students.csv");
CommandResult result = importCommand.execute(model);
assertEquals(String.format(ImportCommand.MESSAGE_SUCCESS, 1, 0), result.getFeedbackToUser());
}

@Test
public void resolveFilePath_existingDirectPath_returnsDirectPath() throws IOException {
// Create a file in the current directory
Path directPath = Paths.get("test.csv");
Files.writeString(directPath, "test content");
filesToCleanup.add(directPath);

ImportCommand testCommand = new ImportCommand("test.csv");
Path resolved = testCommand.resolveFilePath("test.csv");
assertEquals(directPath.normalize(), resolved);

Files.deleteIfExists(directPath); // Clean up immediately
}
}
Loading