87 lines
4.0 KiB
C#
87 lines
4.0 KiB
C#
using PLCSharp.Models;
|
|
using PLCSharp.VVMs.Recipe;
|
|
using PLCSharp.VVMs.Workflows;
|
|
using System.IO;
|
|
|
|
namespace PLCSharp.Tests;
|
|
|
|
public class ProjectDirectoryTests
|
|
{
|
|
[Fact]
|
|
public void CopiedDirectoryOpensIndependentlyAndOnlyChangedScriptIsRewritten()
|
|
{
|
|
var sandbox = Directory.CreateTempSubdirectory();
|
|
try
|
|
{
|
|
var source = Path.Combine(sandbox.FullName, "工站 A");
|
|
var copy = Path.Combine(sandbox.FullName, "工站 副本");
|
|
var project = ProjectDirectory.New();
|
|
var second = new Recipe { Name = "配方 B", Comment = "测试备注" };
|
|
project.Recipes.Add(second);
|
|
var flow = new Workflow { Name = "送料", RecipeID = second.ID, Comment = "循环",
|
|
CycleDelayTime = 0.125, Code = "// 独立脚本\r\n", AutomaticExecution = true };
|
|
project.Flows(second.ID).Add(flow);
|
|
project.Save(source);
|
|
foreach (var file in Directory.GetFiles(source, "*", SearchOption.AllDirectories))
|
|
{
|
|
var target = Path.Combine(copy, Path.GetRelativePath(source, file));
|
|
Directory.CreateDirectory(Path.GetDirectoryName(target)!);
|
|
File.Copy(file, target);
|
|
}
|
|
Directory.Move(source, source + ".moved");
|
|
var opened = ProjectDirectory.Open(copy);
|
|
Assert.Equal(project.Id, opened.Id);
|
|
Assert.Equal(project.DefaultRecipeId, opened.DefaultRecipeId);
|
|
Assert.Equal("测试备注", opened.Recipes.Single(r => r.ID == second.ID).Comment);
|
|
var script = Assert.Single(opened.Flows(second.ID));
|
|
Assert.Equal(flow.ID, script.ID);
|
|
Assert.Equal(flow.Code, script.Code);
|
|
Assert.Equal(flow.Name, script.Name);
|
|
Assert.Equal(flow.CycleDelayTime, script.CycleDelayTime);
|
|
Assert.True(script.AutomaticExecution);
|
|
Assert.False(opened.HasChanges);
|
|
|
|
var before = Directory.GetFiles(copy, "*", SearchOption.AllDirectories).ToDictionary(p => p, File.ReadAllText);
|
|
script.Code += "// edit\n";
|
|
opened.Save();
|
|
var changed = Assert.Single(before.Where(p => File.ReadAllText(p.Key) != p.Value));
|
|
Assert.EndsWith(".cs", changed.Key);
|
|
Assert.Equal(script.Code, ProjectDirectory.Open(copy).Flows(second.ID).Single().Code);
|
|
File.WriteAllText(changed.Key, "// external edit");
|
|
Assert.Throws<IOException>(() => opened.Save());
|
|
Assert.Equal("// external edit", File.ReadAllText(changed.Key));
|
|
File.Delete(changed.Key);
|
|
Assert.Throws<FileNotFoundException>(() => ProjectDirectory.Open(copy));
|
|
}
|
|
finally { sandbox.Delete(true); }
|
|
}
|
|
|
|
[Fact]
|
|
public void FailedMultiFileSaveRestoresOriginalScriptAndCanBeRetried()
|
|
{
|
|
var folder = Directory.CreateTempSubdirectory();
|
|
try
|
|
{
|
|
var project = ProjectDirectory.New();
|
|
var flows = project.Flows(project.DefaultRecipeId);
|
|
var first = new Workflow { Name = "First", Code = "// original" };
|
|
flows.Add(first);
|
|
project.Save(folder.FullName);
|
|
|
|
first.Code = "// updated";
|
|
var next = new Workflow { Name = "Second", Code = "// second" };
|
|
flows.Add(next);
|
|
var scripts = Path.GetDirectoryName(Directory.GetFiles(folder.FullName, "*.cs", SearchOption.AllDirectories).Single())!;
|
|
var occupiedPath = Directory.CreateDirectory(Path.Combine(scripts, next.ID + ".cs"));
|
|
Assert.Throws<IOException>(() => project.Save());
|
|
Assert.Equal("// original", File.ReadAllText(Path.Combine(scripts, first.ID + ".cs")));
|
|
Assert.Single(ProjectDirectory.Open(folder.FullName).Flows(project.DefaultRecipeId));
|
|
|
|
occupiedPath.Delete();
|
|
project.Save();
|
|
Assert.Equal(2, ProjectDirectory.Open(folder.FullName).Flows(project.DefaultRecipeId).Count);
|
|
}
|
|
finally { folder.Delete(true); }
|
|
}
|
|
}
|