Skip to content

Commit

Permalink
Prompt user to save dirty projects
Browse files Browse the repository at this point in the history
  • Loading branch information
CharliePoole committed May 16, 2024
1 parent fe211b9 commit 7fdfa52
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 13 deletions.
19 changes: 9 additions & 10 deletions src/TestCentric/testcentric.gui/Presenters/TestCentricPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,9 @@ void OnRunFinished(ResultNode result)
_model.StopTestRun(true);
}

if (CloseProject() == DialogResult.Cancel)
e.Cancel = true;
CloseProject();
//if (CloseProject() == DialogResult.Cancel)
// e.Cancel = true;
}

if (!e.Cancel)
Expand Down Expand Up @@ -641,8 +642,6 @@ private void SaveProject()
try
{
_model.SaveProject(projectPath);

_view.MessageDisplay.Info(String.Format($"Results saved to {projectPath}"));
}
catch (Exception exception)
{
Expand All @@ -655,15 +654,15 @@ private void SaveProject()

#region Close Methods

public DialogResult CloseProject()
public void CloseProject()
{
//DialogResult result = SaveProjectIfDirty();
if (!_options.Unattended && _model.TestCentricProject.IsDirty &&
_view.MessageDisplay.YesNo($"Do you want to save {_model.TestCentricProject.Name}?"))
{
SaveProject();
}

//if (result != DialogResult.Cancel)
_model.CloseProject();

//return result;
return DialogResult.OK;
}

#endregion
Expand Down
12 changes: 10 additions & 2 deletions src/TestCentric/tests/Presenters/Main/CommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace TestCentric.Gui.Presenters.Main
using Elements;
using Views;
using Model;
using NSubstitute.Core.Arguments;

public class CommandTests : MainPresenterTestBase
{
Expand Down Expand Up @@ -116,10 +117,17 @@ public void AddTestFilesCommand_WhenNothingIsSelected_DoesNotCreateProject()
_model.DidNotReceiveWithAnyArgs().CreateNewProject(null);
}

[Test]
public void CloseProjectCommand_CallsCloseProject()
[TestCase(false)]
[TestCase(true)]
public void CloseProjectCommand_CallsCloseProject(bool dirty)
{
var project = new TestCentricProject(_model, "dummy.dll");
if (dirty) project.AddSetting("SomeSetting", "VALUE");
_model.TestCentricProject.Returns(project);
_view.MessageDisplay.YesNo(Arg.Any<string>()).Returns(false);

_view.CloseProjectCommand.Execute += Raise.Event<CommandHandler>();

_model.Received().CloseProject();
}

Expand Down
22 changes: 22 additions & 0 deletions src/TestModel/model/TestCentricProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class TestCentricProject : TestPackage

public IList<String> TestFiles { get; private set; }

public bool IsDirty { get; private set; }

public TestCentricProject(ITestModel model)
{
_model = model;
Expand All @@ -37,6 +39,7 @@ public TestCentricProject(ITestModel model, IList<string> filenames)
{
_model = model;
TestFiles = filenames;
IsDirty = true;

var engineSettings = _model.Settings.Engine;
var options = model.Options;
Expand Down Expand Up @@ -136,6 +139,8 @@ public void Save(TextWriter writer)
{
throw new Exception("Unable to serialize TestProject.", ex);
}

IsDirty = false;
}

public void LoadTests()
Expand All @@ -147,5 +152,22 @@ public void UnloadTests()
{

}

public new void AddSubPackage(string fullName)
{
base.AddSubPackage(fullName);
IsDirty = true;
}
public new void AddSubPackage(TestPackage subPackage)
{
base.AddSubPackage(subPackage);
IsDirty = true;
}

public new void AddSetting(string key, object value)
{
base.AddSetting(key, value);
IsDirty = true;
}
}
}
51 changes: 50 additions & 1 deletion src/TestModel/tests/CreateNewProjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using NUnit.Framework;
using TestCentric.Common;
using TestCentric.Gui.Model.Fakes;
Expand All @@ -23,6 +23,13 @@ public void CreateModel()
_model = new TestModel(new MockTestEngine());
}

[TearDown]
public void TearDown()
{
if (File.Exists("temp.tcproj"))
File.Delete("temp.tcproj");
}

[TestCase("my.test.assembly.dll")]
[TestCase("one.dll", "two.dll", "three.dll")]
[TestCase("tests.nunit")]
Expand Down Expand Up @@ -103,5 +110,47 @@ public void PackageForSolutionFileHasSkipNonTestAssemblies(string files)
Assert.That(subpackage.Settings, Does.Not.ContainKey(skipKey));
}
}


[Test]
public void NewProjectIsDirty()
{
var project = new TestCentricProject(_model, new[] { "dummy.dll" });
Assert.That(project.IsDirty);
}

public void NewProjectIsNotDirtyAfterSaving()
{
var project = new TestCentricProject(_model, new[] { "dummy.dll" });
project.SaveAs("temp.tcproj");
Assert.That(project.IsDirty, Is.False);
}

[Test]
public void LoadedProjectIsNotDirty()
{
var project = new TestCentricProject(_model);
project.SaveAs("temp.tcproj");
project.Load("temp.tcproj");
Assert.That(project.IsDirty, Is.False);
}

[Test]
public void AddingSubProjectMakesProjectDirty()
{
var project = _model.CreateNewProject(new[] { "dummy.dll" });
project.SaveAs("temp.tcproj");
project.AddSubPackage("another.dll");
Assert.That(project.IsDirty);
}

[Test]
public void AddingSettingMakesProjectDirty()
{
var project = _model.CreateNewProject(new[] { "dummy.dll" });
project.SaveAs("temp.tcproj");
project.AddSetting("NewSetting", "VALUE");
Assert.That(project.IsDirty);
}
}
}

0 comments on commit 7fdfa52

Please sign in to comment.