mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-27 23:11:34 -05:00
feat(desktop): save a nest tab to a file or export a copy
NestTabs.SaveAs writes the tab's nest, makes the file its file and name, and clears its unsaved mark; a write that fails throws and changes nothing. ExportCopy writes the nest without touching its file, name or unsaved mark. Both refuse, before writing anything, a file another open tab shows (so one file never has two tabs) and a tab that has been closed; Export also refuses the tab's own file, which only Save may write. Refusals are a SaveTargetRefusedException whose message says what to do. SavePath is the file Save writes without asking: only one saved to this session, as in the WinForms app, so a nest that was only opened asks for a file on its first Save. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fa6781d8b1
commit
8e87c0881a
3 files changed
+272
-2
No files matched your search
@@ -0,0 +1,195 @@
|
||||
using OpenNest.IO;
|
||||
|
||||
namespace OpenNest.Desktop.Tests;
|
||||
|
||||
public sealed class NestTabsSaveTests : IDisposable
|
||||
{
|
||||
private readonly string directory =
|
||||
Directory.CreateTempSubdirectory("opennest-desktop-tabs-save-").FullName;
|
||||
|
||||
public void Dispose() => Directory.Delete(directory, recursive: true);
|
||||
|
||||
private string InDir(string name) => Path.Combine(directory, name);
|
||||
|
||||
// Opens a real nest file in a new tab, as File > Open does.
|
||||
private NestTab OpenSaved(NestTabs tabs, string name)
|
||||
{
|
||||
var path = InDir(name);
|
||||
var nest = TestNests.OneSquare();
|
||||
nest.Name = Path.GetFileNameWithoutExtension(name);
|
||||
Assert.True(new NestWriter(nest).Write(path));
|
||||
return tabs.Open(path, file => new Document { Nest = new NestReader(file).Read() });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SavePath_IsNullForANestOnlyOpened_SoTheFirstSaveAsks()
|
||||
{
|
||||
var tabs = new NestTabs();
|
||||
|
||||
var tab = OpenSaved(tabs, "opened.nest");
|
||||
|
||||
Assert.Null(tab.SavePath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SaveAs_WritesTheFileAndMakesItTheTabsFileNameAndSavePath()
|
||||
{
|
||||
var tabs = new NestTabs();
|
||||
var tab = OpenSaved(tabs, "opened.nest");
|
||||
tab.MarkModified();
|
||||
var changed = 0;
|
||||
tabs.Changed += (_, _) => changed++;
|
||||
var target = InDir("Renamed Job.nest");
|
||||
|
||||
tabs.SaveAs(tab, target);
|
||||
|
||||
Assert.Equal(target, tab.Path);
|
||||
Assert.Equal(target, tab.SavePath);
|
||||
Assert.Equal("Renamed Job", tab.Name);
|
||||
Assert.False(tab.Modified);
|
||||
Assert.Equal(1, changed);
|
||||
var read = new NestReader(target).Read();
|
||||
Assert.Equal("Renamed Job", read.Name);
|
||||
Assert.Single(read.Plates[0].Parts);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SaveAs_ThenOpeningTheSavedFile_SwitchesToTheTabInsteadOfOpeningItTwice()
|
||||
{
|
||||
var tabs = new NestTabs();
|
||||
var tab = OpenSaved(tabs, "opened.nest");
|
||||
OpenSaved(tabs, "other.nest");
|
||||
var target = InDir("saved.nest");
|
||||
tabs.SaveAs(tab, target);
|
||||
|
||||
var again = tabs.Open(target, _ => throw new InvalidOperationException("loaded again"));
|
||||
|
||||
Assert.Same(tab, again);
|
||||
Assert.Equal(2, tabs.Tabs.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SaveAs_AFileAnotherTabShows_IsRefusedAndWritesNothing()
|
||||
{
|
||||
var tabs = new NestTabs();
|
||||
var a = OpenSaved(tabs, "a.nest");
|
||||
var b = OpenSaved(tabs, "b.nest");
|
||||
a.MarkModified();
|
||||
var before = File.ReadAllBytes(b.Path);
|
||||
|
||||
var ex = Assert.Throws<SaveTargetRefusedException>(() => tabs.SaveAs(a, b.Path));
|
||||
|
||||
Assert.Contains("b.nest is open in the b tab", ex.Message);
|
||||
Assert.Equal(before, File.ReadAllBytes(b.Path));
|
||||
Assert.Equal(InDir("a.nest"), a.Path);
|
||||
Assert.Equal("a", a.Name);
|
||||
Assert.True(a.Modified);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SaveAs_OntoItsOwnFile_IsAllowed()
|
||||
{
|
||||
var tabs = new NestTabs();
|
||||
var tab = OpenSaved(tabs, "own.nest");
|
||||
tab.MarkModified();
|
||||
|
||||
tabs.SaveAs(tab, tab.Path);
|
||||
|
||||
Assert.False(tab.Modified);
|
||||
Assert.Equal(InDir("own.nest"), tab.SavePath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SaveAs_AWriteThatFails_KeepsTheFileNameAndUnsavedMark()
|
||||
{
|
||||
var tabs = new NestTabs();
|
||||
var tab = OpenSaved(tabs, "kept.nest");
|
||||
tab.MarkModified();
|
||||
var changed = 0;
|
||||
tabs.Changed += (_, _) => changed++;
|
||||
|
||||
Assert.ThrowsAny<IOException>(
|
||||
() => tabs.SaveAs(tab, InDir(Path.Combine("missing folder", "x.nest"))));
|
||||
|
||||
Assert.Equal(InDir("kept.nest"), tab.Path);
|
||||
Assert.Equal("kept", tab.Name);
|
||||
Assert.Null(tab.Document.LastSavePath);
|
||||
Assert.True(tab.Modified);
|
||||
Assert.Equal(0, changed);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExportCopy_WritesTheNestButKeepsTheTabsFileNameAndUnsavedMark()
|
||||
{
|
||||
var tabs = new NestTabs();
|
||||
var tab = OpenSaved(tabs, "job.nest");
|
||||
tab.MarkModified();
|
||||
var target = InDir("copy.nest");
|
||||
|
||||
tabs.ExportCopy(tab, target);
|
||||
|
||||
Assert.Equal(InDir("job.nest"), tab.Path);
|
||||
Assert.Equal("job", tab.Name);
|
||||
Assert.Null(tab.SavePath);
|
||||
Assert.True(tab.Modified);
|
||||
var read = new NestReader(target).Read();
|
||||
Assert.Equal("job", read.Name);
|
||||
Assert.Single(read.Plates[0].Parts);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SaveAs_AFailedWriteAfterAnEarlierSave_KeepsTheEarlierSavePath()
|
||||
{
|
||||
var tabs = new NestTabs();
|
||||
var tab = OpenSaved(tabs, "first.nest");
|
||||
var earlier = InDir("earlier.nest");
|
||||
tabs.SaveAs(tab, earlier);
|
||||
|
||||
Assert.ThrowsAny<IOException>(
|
||||
() => tabs.SaveAs(tab, InDir(Path.Combine("missing folder", "x.nest"))));
|
||||
|
||||
Assert.Equal(earlier, tab.SavePath);
|
||||
Assert.Equal(earlier, tab.Path);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SaveAs_ATabThatWasClosed_IsRefusedAndWritesNothing()
|
||||
{
|
||||
var tabs = new NestTabs();
|
||||
var tab = OpenSaved(tabs, "closed.nest");
|
||||
tabs.Close(tab);
|
||||
var target = InDir("after-close.nest");
|
||||
|
||||
Assert.Throws<SaveTargetRefusedException>(() => tabs.SaveAs(tab, target));
|
||||
Assert.Throws<SaveTargetRefusedException>(() => tabs.ExportCopy(tab, target));
|
||||
|
||||
Assert.False(File.Exists(target));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExportCopy_OntoTheTabsOwnFile_IsRefusedAndWritesNothing()
|
||||
{
|
||||
var tabs = new NestTabs();
|
||||
var tab = OpenSaved(tabs, "own.nest");
|
||||
tab.Document.Nest.Notes = "not saved";
|
||||
var before = File.ReadAllBytes(tab.Path);
|
||||
|
||||
var ex = Assert.Throws<SaveTargetRefusedException>(() => tabs.ExportCopy(tab, tab.Path));
|
||||
|
||||
Assert.Contains("File > Save", ex.Message);
|
||||
Assert.Equal(before, File.ReadAllBytes(tab.Path));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExportCopy_ToAFileAnotherTabShows_IsRefusedAndWritesNothing()
|
||||
{
|
||||
var tabs = new NestTabs();
|
||||
var a = OpenSaved(tabs, "a.nest");
|
||||
var b = OpenSaved(tabs, "b.nest");
|
||||
var before = File.ReadAllBytes(b.Path);
|
||||
|
||||
Assert.Throws<SaveTargetRefusedException>(() => tabs.ExportCopy(a, b.Path));
|
||||
|
||||
Assert.Equal(before, File.ReadAllBytes(b.Path));
|
||||
}
|
||||
}
|
||||
@@ -77,6 +77,17 @@ namespace OpenNest
|
||||
History.MarkSaved();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a copy of the nest to <paramref name="path"/>. The nest's name, save path,
|
||||
/// server identity and undo save point stay as they were (the writer still stamps its
|
||||
/// last-modified date, as every save does).
|
||||
/// </summary>
|
||||
public void ExportCopy(string path)
|
||||
{
|
||||
// A write that fails throws and leaves any file already at the path as it was.
|
||||
new NestWriter(Nest) { BestFitStore = BestFitCacheStore.Shared }.Write(path);
|
||||
}
|
||||
|
||||
/// <summary>Serializes the current nest to a .nest archive in memory.</summary>
|
||||
public byte[] Serialize()
|
||||
{
|
||||
|
||||
@@ -16,8 +16,14 @@ namespace OpenNest.Desktop
|
||||
|
||||
public Document Document { get; }
|
||||
|
||||
/// <summary>The full path of the file it was opened from, else null.</summary>
|
||||
public string Path { get; }
|
||||
/// <summary>The full path of the file it was opened from or saved to, else null.</summary>
|
||||
public string Path { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// The file Save writes without asking: the one saved to this session, if it still
|
||||
/// exists. Null for a nest only opened, as in the WinForms app, so the first Save asks.
|
||||
/// </summary>
|
||||
public string SavePath => Document.HasSavePath ? Document.LastSavePath : null;
|
||||
|
||||
/// <summary>True after a change this session that is not saved.</summary>
|
||||
public bool Modified { get; private set; }
|
||||
@@ -32,6 +38,18 @@ namespace OpenNest.Desktop
|
||||
public void MarkSaved() => Modified = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A save or export refused before anything was written; the message says why and what to
|
||||
/// do instead.
|
||||
/// </summary>
|
||||
public sealed class SaveTargetRefusedException : InvalidOperationException
|
||||
{
|
||||
public SaveTargetRefusedException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The nests open in the desktop app, one tab each, and which one is active. Opening a file
|
||||
/// that is already open activates its tab; closing the active tab activates the one to its
|
||||
@@ -104,6 +122,52 @@ namespace OpenNest.Desktop
|
||||
Changed?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the tab's nest to <paramref name="path"/>, which becomes its file and name, and
|
||||
/// clears its unsaved mark. Refuses a file another open tab shows, so one file never has
|
||||
/// two tabs. A failed write throws and changes nothing.
|
||||
/// </summary>
|
||||
public void SaveAs(NestTab tab, string path)
|
||||
{
|
||||
var fullPath = CheckTarget(tab, path, export: false);
|
||||
tab.Document.SaveAs(fullPath);
|
||||
tab.Path = fullPath;
|
||||
tab.MarkSaved();
|
||||
Changed?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a copy of the tab's nest to <paramref name="path"/> without changing its file,
|
||||
/// name or unsaved mark. Refuses the tab's own file, which only Save may write, and a
|
||||
/// file another open tab shows.
|
||||
/// </summary>
|
||||
public void ExportCopy(NestTab tab, string path)
|
||||
{
|
||||
var fullPath = CheckTarget(tab, path, export: true);
|
||||
tab.Document.ExportCopy(fullPath);
|
||||
}
|
||||
|
||||
private string CheckTarget(NestTab tab, string path, bool export)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(tab);
|
||||
ArgumentNullException.ThrowIfNull(path);
|
||||
if (!tabs.Contains(tab))
|
||||
throw new SaveTargetRefusedException(
|
||||
$"{tab.Name} was closed before it could be written. Open it again to save it.");
|
||||
var fullPath = System.IO.Path.GetFullPath(path);
|
||||
if (export && tab.Path != null && SamePath(tab.Path, fullPath))
|
||||
throw new SaveTargetRefusedException(
|
||||
$"{System.IO.Path.GetFileName(fullPath)} is this nest's own file. Use "
|
||||
+ "File > Save to save it there, or export the copy under another name.");
|
||||
var other = tabs.FirstOrDefault(
|
||||
t => t != tab && t.Path != null && SamePath(t.Path, fullPath));
|
||||
if (other != null)
|
||||
throw new SaveTargetRefusedException(
|
||||
$"{System.IO.Path.GetFileName(fullPath)} is open in the {other.Name} tab. "
|
||||
+ "Close that tab first, or choose another file name.");
|
||||
return fullPath;
|
||||
}
|
||||
|
||||
// Windows paths ignore case; Linux and macOS paths do not.
|
||||
private static bool SamePath(string a, string b) => string.Equals(a, b,
|
||||
OperatingSystem.IsWindows()
|
||||
|
||||
Reference in new issue
Block a user