Skip to content

Commit

Permalink
Handle empty session file
Browse files Browse the repository at this point in the history
  • Loading branch information
dariober committed Aug 3, 2024
1 parent 6944e41 commit 5d34b56
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ New in 1.19.0
* Support for sessions via `session*` commands. Similar to other genome
browsers, ASCIIGenome can save a session to be re-opened later. Sessions are
saved in a yaml file (default `~/.asciigenome/session.yml`) which is a list
of dictionaries (each dictionary is a session). At present, only some basic
of dictionaries (each dictionary is a session). If desired, these files
should be fairly easy to edit with a text editor. At present, only some basic
configurations are saved and things could change in the future.

Current session* commands, use `<command> -h` for more detail:
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/session/SessionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
Expand Down Expand Up @@ -41,6 +42,11 @@ private static List<Session> read(File sessionYamlFile) throws IOException, Sess
throw new SessionException(
"File '" + sessionYamlFile.getAbsolutePath() + "' does not exist or is not readable.");
}
String content =
new String(Files.readAllBytes(sessionYamlFile.toPath()), StandardCharsets.UTF_8);
if (content.trim().isEmpty()) {
return new ArrayList<Session>();
}
InputStream yaml = Files.newInputStream(Paths.get(sessionYamlFile.getPath()));
List<Session> sessions =
new YAMLMapper().readValue(yaml, new TypeReference<List<Session>>() {});
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/samTextViewer/InteractiveInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ public void canListCurrentSessionNameAndFile()

ProcessInput pi = processInput(ip, "sessionList", proc);
assertTrue(
Pattern.compile("Session file: .*/.asciigenome/session.yml").matcher(pi.stderr).find());
Pattern.compile("Session file: /.*/.asciigenome/session.yml").matcher(pi.stderr).find());
assertTrue(pi.stderr.contains("Current session: n/a"));

ip.processInput("sessionOpen -f test_data/session.yaml no-fastafile", proc);
pi = processInput(ip, "sessionList -f test_data/session.yaml no-fastafile", proc);
assertTrue(
Pattern.compile("Session file: .*/test_data/session.yaml").matcher(pi.stderr).find());
Pattern.compile("Session file: /.*/test_data/session.yaml").matcher(pi.stderr).find());
assertTrue(pi.stderr.contains("Current session: no-fastafile"));
}

Expand Down

0 comments on commit 5d34b56

Please sign in to comment.