diff --git a/changelog.md b/changelog.md index 6bc710b..7b1a908 100644 --- a/changelog.md +++ b/changelog.md @@ -4,8 +4,10 @@ - Fixed: If note is played via a qwerty key press, and then an octave is changed via a qwerty key press, there won't be a note-off event. - Fixed: Cacophony can't found files (saves, soundfonts, etc.) if the file extension contains uppercase characters. -- Fixed clippy warnings for Rust 1.78 -- The GitHub workflow for building Cacophony now uses the latest version of Rust. +- Fixed: ChildPaths sometimes doesn't set the correct directory when moving up a directory. +- (Backend) Fixed clippy warnings for Rust 1.78 +- (Backend) The GitHub workflow for building Cacophony now uses the latest version of Rust. +- (Backend) Added tests for ChildPaths. ## 0.2.3 diff --git a/common/src/open_file/child_paths.rs b/common/src/open_file/child_paths.rs index c91705b..a3c8039 100644 --- a/common/src/open_file/child_paths.rs +++ b/common/src/open_file/child_paths.rs @@ -33,7 +33,16 @@ impl ChildPaths { self.selected = children .iter() .enumerate() - .filter(|p| p.1.path == previous_directory) + .filter(|p| { + p.1.stem + == previous_directory + .components() + .last() + .unwrap() + .as_os_str() + .to_str() + .unwrap() + }) .map(|p| p.0) .next(); } @@ -91,3 +100,89 @@ impl ChildPaths { paths } } + +#[cfg(test)] +mod tests { + use std::{fs::canonicalize, path::PathBuf}; + + use super::ChildPaths; + use crate::open_file::{Extension, FileOrDirectory}; + + #[test] + fn test_sf2_child_paths() { + let sf_directory = PathBuf::from("../data"); + assert!(sf_directory.exists()); + let mut child_paths = ChildPaths::default(); + child_paths.set(&sf_directory, &Extension::Sf2, None); + assert_eq!(child_paths.children.len(), 1); + let f = &child_paths.children[0]; + assert!(f.is_file); + assert_eq!(f.stem, "CT1MBGMRSV1.06.sf2"); + assert!(child_paths.selected.is_some()); + assert_eq!(child_paths.selected.unwrap(), 0); + // There shouldn't be any save files. + child_paths.set(&sf_directory, &Extension::Cac, None); + assert!(child_paths.children.is_empty()); + assert!(child_paths.selected.is_some()); + assert_eq!(child_paths.selected.unwrap(), 0); + let parent_directory = canonicalize(sf_directory.parent().unwrap()).unwrap(); + assert_eq!( + parent_directory + .components() + .last() + .unwrap() + .as_os_str() + .to_str() + .unwrap(), + "cacophony" + ); + // Set a different directory. + child_paths.set(&parent_directory, &Extension::Sf2, None); + assert!(!child_paths.children.is_empty()); + assert!(child_paths + .children + .iter() + .filter(|c| c.is_file) + .collect::>() + .is_empty()); + // Ignore any folders that have names beginning with a period because they won't all appear in the GitHub workflow. + child_paths + .children + .retain(|f| match f.stem.chars().next() { + Some(ch) => ch != '.', + None => false, + }); + assert_eq!(child_paths.children.len(), 14); + assert!(child_paths.selected.is_some()); + assert_eq!(child_paths.selected.unwrap(), 0); + // Go "up" a directory. + child_paths.set( + &parent_directory, + &Extension::Sf2, + Some(sf_directory.clone()), + ); + // Test the selection. + assert_eq!( + child_paths.children[child_paths.selected.unwrap()].stem, + "data" + ); + } + + #[test] + fn test_cac_child_paths() { + let cac_directory = PathBuf::from("../test_files/child_paths"); + assert!(cac_directory.exists()); + let mut child_paths = ChildPaths::default(); + child_paths.set(&cac_directory, &Extension::Cac, None); + assert_eq!(child_paths.children.len(), 3); + test_cac_file(&child_paths, child_paths.selected.unwrap(), "test_0.cac"); + test_cac_file(&child_paths, 1, "test_1.cac"); + test_cac_file(&child_paths, 2, "test_2.CAC"); + } + + fn test_cac_file(child_paths: &ChildPaths, index: usize, filename: &str) { + let f = &child_paths.children[index]; + assert!(f.is_file); + assert_eq!(f.stem, filename); + } +} diff --git a/test_files/child_paths/test_0.cac b/test_files/child_paths/test_0.cac new file mode 100644 index 0000000..ca6cbd1 --- /dev/null +++ b/test_files/child_paths/test_0.cac @@ -0,0 +1 @@ +{"state":{"music":{"midi_tracks":[{"channel":0,"gain":127,"notes":[[60,127,0,192],[62,127,192,384],[64,127,384,576]],"mute":false,"solo":false}],"selected":0},"view":{"dt":[0,25536],"dn":[75,45],"mode":{"values":["Normal","Quick","Precise"],"index":{"index":0,"length":3}},"single_track":true,"zoom_levels":[404,462,528,604,691,790,903,1033,1181,1350,1543,1764,2016,2304,2634,3011,3442,3934,4497,5140,5875,6715,7675,8772,10026,11459,13097,14968,17107,19551,22344,25536,29184,33353,38117,43562,49785,56897,65025,74314,84930,97062,110928,126774,144884,165581,189235,216268,247163,282472,322825,368942,421648,481883,550723,629397,719310,822068,939506,1073721,1227109,1402410,1602754,1831718],"zoom_index":{"index":31,"length":64},"zoom_increments":{"Quick":4,"Normal":2,"Precise":1},"initial_zoom_index":31},"time":{"cursor":576,"playback":0,"bpm":120,"mode":{"values":["Normal","Quick","Precise"],"index":{"index":0,"length":3}}},"input":{"armed":true,"alphanumeric_input":false,"volume":{"index":127,"length":128},"use_volume":true,"beat":192},"panels":["Music","Tracks","PianoRoll"],"focus":{"index":2,"length":3},"music_panel_field":{"values":["Name","BPM","Gain"],"index":{"index":0,"length":3}},"piano_roll_mode":"Time","edit_mode":{"values":["Normal","Quick","Precise"],"index":{"index":0,"length":3}},"select_mode":{"Single":null}},"synth_state":{"programs":{"0":{"path":"/home/esther/cacophony/data/CT1MBGMRSV1.06.sf2","num_banks":2,"bank_index":0,"bank":0,"num_presets":128,"preset":0,"preset_index":0,"preset_name":"Piano 1"}},"gain":127},"paths_state":{"soundfonts":{"directory":{"path":"/home/esther/Documents/cacophony/soundfonts","is_file":false,"stem":"soundfonts"},"filename":null},"saves":{"directory":{"path":"/home/esther/Documents/cacophony/saves","is_file":false,"stem":"saves"},"filename":"uh"},"exports":{"directory":{"path":"/home/esther/Documents/cacophony/exports","is_file":false,"stem":"exports"},"filename":null},"midis":{"directory":{"path":"/home/esther/Documents/cacophony","is_file":false,"stem":"cacophony"},"filename":null}},"exporter":{"framerate":44100,"metadata":{"title":"My Music","artist":null,"album":null,"track_number":null,"genre":null,"comment":null},"copyright":false,"mp3_bit_rate":{"index":12,"length":16},"mp3_quality":{"index":9,"length":10},"multi_file":false,"multi_file_suffix":{"values":["ChannelAndPreset","Preset","Channel"],"index":{"index":0,"length":3}},"ogg_quality":{"index":9,"length":10},"export_type":{"values":["Wav","Mid","MP3","Ogg","Flac"],"index":{"index":0,"length":5}},"mid_settings":{"values":["Title","Artist","Copyright"],"index":{"index":0,"length":3}},"wav_settings":{"values":["Framerate","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":3}},"mp3_settings":{"values":["Framerate","Mp3Quality","Mp3BitRate","Title","Artist","Copyright","Album","TrackNumber","Genre","Comment","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":12}},"ogg_settings":{"values":["Framerate","OggQuality","Title","Artist","Copyright","Album","TrackNumber","Genre","Comment","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":11}},"flac_settings":{"values":["Framerate","Title","Artist","Copyright","Album","TrackNumber","Genre","Comment","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":10}}},"version":"0.2.4"} \ No newline at end of file diff --git a/test_files/child_paths/test_1.cac b/test_files/child_paths/test_1.cac new file mode 100644 index 0000000..ca6cbd1 --- /dev/null +++ b/test_files/child_paths/test_1.cac @@ -0,0 +1 @@ +{"state":{"music":{"midi_tracks":[{"channel":0,"gain":127,"notes":[[60,127,0,192],[62,127,192,384],[64,127,384,576]],"mute":false,"solo":false}],"selected":0},"view":{"dt":[0,25536],"dn":[75,45],"mode":{"values":["Normal","Quick","Precise"],"index":{"index":0,"length":3}},"single_track":true,"zoom_levels":[404,462,528,604,691,790,903,1033,1181,1350,1543,1764,2016,2304,2634,3011,3442,3934,4497,5140,5875,6715,7675,8772,10026,11459,13097,14968,17107,19551,22344,25536,29184,33353,38117,43562,49785,56897,65025,74314,84930,97062,110928,126774,144884,165581,189235,216268,247163,282472,322825,368942,421648,481883,550723,629397,719310,822068,939506,1073721,1227109,1402410,1602754,1831718],"zoom_index":{"index":31,"length":64},"zoom_increments":{"Quick":4,"Normal":2,"Precise":1},"initial_zoom_index":31},"time":{"cursor":576,"playback":0,"bpm":120,"mode":{"values":["Normal","Quick","Precise"],"index":{"index":0,"length":3}}},"input":{"armed":true,"alphanumeric_input":false,"volume":{"index":127,"length":128},"use_volume":true,"beat":192},"panels":["Music","Tracks","PianoRoll"],"focus":{"index":2,"length":3},"music_panel_field":{"values":["Name","BPM","Gain"],"index":{"index":0,"length":3}},"piano_roll_mode":"Time","edit_mode":{"values":["Normal","Quick","Precise"],"index":{"index":0,"length":3}},"select_mode":{"Single":null}},"synth_state":{"programs":{"0":{"path":"/home/esther/cacophony/data/CT1MBGMRSV1.06.sf2","num_banks":2,"bank_index":0,"bank":0,"num_presets":128,"preset":0,"preset_index":0,"preset_name":"Piano 1"}},"gain":127},"paths_state":{"soundfonts":{"directory":{"path":"/home/esther/Documents/cacophony/soundfonts","is_file":false,"stem":"soundfonts"},"filename":null},"saves":{"directory":{"path":"/home/esther/Documents/cacophony/saves","is_file":false,"stem":"saves"},"filename":"uh"},"exports":{"directory":{"path":"/home/esther/Documents/cacophony/exports","is_file":false,"stem":"exports"},"filename":null},"midis":{"directory":{"path":"/home/esther/Documents/cacophony","is_file":false,"stem":"cacophony"},"filename":null}},"exporter":{"framerate":44100,"metadata":{"title":"My Music","artist":null,"album":null,"track_number":null,"genre":null,"comment":null},"copyright":false,"mp3_bit_rate":{"index":12,"length":16},"mp3_quality":{"index":9,"length":10},"multi_file":false,"multi_file_suffix":{"values":["ChannelAndPreset","Preset","Channel"],"index":{"index":0,"length":3}},"ogg_quality":{"index":9,"length":10},"export_type":{"values":["Wav","Mid","MP3","Ogg","Flac"],"index":{"index":0,"length":5}},"mid_settings":{"values":["Title","Artist","Copyright"],"index":{"index":0,"length":3}},"wav_settings":{"values":["Framerate","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":3}},"mp3_settings":{"values":["Framerate","Mp3Quality","Mp3BitRate","Title","Artist","Copyright","Album","TrackNumber","Genre","Comment","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":12}},"ogg_settings":{"values":["Framerate","OggQuality","Title","Artist","Copyright","Album","TrackNumber","Genre","Comment","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":11}},"flac_settings":{"values":["Framerate","Title","Artist","Copyright","Album","TrackNumber","Genre","Comment","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":10}}},"version":"0.2.4"} \ No newline at end of file diff --git a/test_files/child_paths/test_2.CAC b/test_files/child_paths/test_2.CAC new file mode 100644 index 0000000..ca6cbd1 --- /dev/null +++ b/test_files/child_paths/test_2.CAC @@ -0,0 +1 @@ +{"state":{"music":{"midi_tracks":[{"channel":0,"gain":127,"notes":[[60,127,0,192],[62,127,192,384],[64,127,384,576]],"mute":false,"solo":false}],"selected":0},"view":{"dt":[0,25536],"dn":[75,45],"mode":{"values":["Normal","Quick","Precise"],"index":{"index":0,"length":3}},"single_track":true,"zoom_levels":[404,462,528,604,691,790,903,1033,1181,1350,1543,1764,2016,2304,2634,3011,3442,3934,4497,5140,5875,6715,7675,8772,10026,11459,13097,14968,17107,19551,22344,25536,29184,33353,38117,43562,49785,56897,65025,74314,84930,97062,110928,126774,144884,165581,189235,216268,247163,282472,322825,368942,421648,481883,550723,629397,719310,822068,939506,1073721,1227109,1402410,1602754,1831718],"zoom_index":{"index":31,"length":64},"zoom_increments":{"Quick":4,"Normal":2,"Precise":1},"initial_zoom_index":31},"time":{"cursor":576,"playback":0,"bpm":120,"mode":{"values":["Normal","Quick","Precise"],"index":{"index":0,"length":3}}},"input":{"armed":true,"alphanumeric_input":false,"volume":{"index":127,"length":128},"use_volume":true,"beat":192},"panels":["Music","Tracks","PianoRoll"],"focus":{"index":2,"length":3},"music_panel_field":{"values":["Name","BPM","Gain"],"index":{"index":0,"length":3}},"piano_roll_mode":"Time","edit_mode":{"values":["Normal","Quick","Precise"],"index":{"index":0,"length":3}},"select_mode":{"Single":null}},"synth_state":{"programs":{"0":{"path":"/home/esther/cacophony/data/CT1MBGMRSV1.06.sf2","num_banks":2,"bank_index":0,"bank":0,"num_presets":128,"preset":0,"preset_index":0,"preset_name":"Piano 1"}},"gain":127},"paths_state":{"soundfonts":{"directory":{"path":"/home/esther/Documents/cacophony/soundfonts","is_file":false,"stem":"soundfonts"},"filename":null},"saves":{"directory":{"path":"/home/esther/Documents/cacophony/saves","is_file":false,"stem":"saves"},"filename":"uh"},"exports":{"directory":{"path":"/home/esther/Documents/cacophony/exports","is_file":false,"stem":"exports"},"filename":null},"midis":{"directory":{"path":"/home/esther/Documents/cacophony","is_file":false,"stem":"cacophony"},"filename":null}},"exporter":{"framerate":44100,"metadata":{"title":"My Music","artist":null,"album":null,"track_number":null,"genre":null,"comment":null},"copyright":false,"mp3_bit_rate":{"index":12,"length":16},"mp3_quality":{"index":9,"length":10},"multi_file":false,"multi_file_suffix":{"values":["ChannelAndPreset","Preset","Channel"],"index":{"index":0,"length":3}},"ogg_quality":{"index":9,"length":10},"export_type":{"values":["Wav","Mid","MP3","Ogg","Flac"],"index":{"index":0,"length":5}},"mid_settings":{"values":["Title","Artist","Copyright"],"index":{"index":0,"length":3}},"wav_settings":{"values":["Framerate","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":3}},"mp3_settings":{"values":["Framerate","Mp3Quality","Mp3BitRate","Title","Artist","Copyright","Album","TrackNumber","Genre","Comment","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":12}},"ogg_settings":{"values":["Framerate","OggQuality","Title","Artist","Copyright","Album","TrackNumber","Genre","Comment","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":11}},"flac_settings":{"values":["Framerate","Title","Artist","Copyright","Album","TrackNumber","Genre","Comment","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":10}}},"version":"0.2.4"} \ No newline at end of file