Skip to content

Commit

Permalink
feat: error handling in Config.CreateFromSolution and AutomationInter…
Browse files Browse the repository at this point in the history
…faceHeadless to deal with 'solution does not exist'
  • Loading branch information
iadonkey committed Sep 14, 2024
1 parent 0096e3f commit d7eebe7
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
3 changes: 3 additions & 0 deletions TwinpackCore/Configuration/ConfigFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ public static async Task<Config> CreateFromSolutionFileAsync(string path=".", bo
}
}

if (solution?.Projects?.FirstOrDefault()?.Plcs?.FirstOrDefault() == null)
return null;

foreach (var project in solution.Projects)
{
var projectConfig = new ConfigProject();
Expand Down
11 changes: 11 additions & 0 deletions TwinpackCore/Core/AutomationInterfaceHeadless.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ static void AddOptions(XElement root, AddPlcLibraryOptions options)
await RemovePackageAsync(package, forceRemoval: true);

var plcConfig = _config.Projects.FirstOrDefault(x => x.Name == package.ProjectName).Plcs.FirstOrDefault(x => x.Name == package.PlcName);
if (plcConfig.FilePath == null || !File.Exists(plcConfig.FilePath))
throw new FileNotFoundException($"Plc '{plcConfig.Name}' can not be found {(plcConfig.FilePath == null ? "" : "in " + plcConfig.FilePath)}");

var xdoc = XDocument.Load(plcConfig.FilePath);
var project = xdoc.Elements(TcNs + "Project").FirstOrDefault();
Expand Down Expand Up @@ -163,6 +165,9 @@ public override async System.Threading.Tasks.Task RemovePackageAsync(PackageItem
if (plcConfig == null)
throw new InvalidOperationException($"Project '{package.ProjectName}' (Plc {package.PlcName}) is not configured in {_config.FilePath}");

if (plcConfig.FilePath == null || !File.Exists(plcConfig.FilePath))
throw new FileNotFoundException($"Plc '{plcConfig.Name}' can not be found {(plcConfig.FilePath == null ? "" : "in " + plcConfig.FilePath)}");

var xdoc = XDocument.Load(plcConfig.FilePath);
var project = xdoc.Elements(TcNs + "Project").FirstOrDefault();
if (project == null)
Expand Down Expand Up @@ -214,6 +219,9 @@ public override async System.Threading.Tasks.Task RemoveAllPackagesAsync(string
{
var plcConfig = _config.Projects.FirstOrDefault(x => x.Name == projectName).Plcs.FirstOrDefault(x => x.Name == plcName);

if (plcConfig.FilePath == null || !File.Exists(plcConfig.FilePath))
throw new FileNotFoundException($"Plc '{plcConfig.Name}' can not be found {(plcConfig.FilePath == null ? "" : "in " + plcConfig.FilePath)}");

var xdoc = XDocument.Load(plcConfig.FilePath);
var project = xdoc.Elements(TcNs + "Project").FirstOrDefault();
if (project == null)
Expand Down Expand Up @@ -248,6 +256,9 @@ public override async Task<bool> UninstallPackageAsync(PackageItem package)

public override async System.Threading.Tasks.Task SetPackageVersionAsync(ConfigPlcProject plc, CancellationToken cancellationToken = default)
{
if (plc.FilePath == null || !File.Exists(plc.FilePath))
throw new FileNotFoundException($"Plc '{plc.Name}' can not be found {(plc.FilePath == null ? "" : "in " + plc.FilePath)}");

var xdoc = XDocument.Load(plc.FilePath);
var project = xdoc.Elements(TcNs + "Project").FirstOrDefault();
if (project == null)
Expand Down
3 changes: 1 addition & 2 deletions TwinpackCore/Core/TwinpackService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,8 @@ public async System.Threading.Tasks.Task<List<PackageItem>> AddPackagesAsync(Lis
cancellationToken.ThrowIfCancellationRequested();
}


// add affected packages as references
foreach (var package in options?.IncludeDependencies == true ? affectedPackages : packages)
foreach (var package in (options?.IncludeDependencies == true ? affectedPackages : packages).Where(x => x.PackageVersion?.Name != null))
{
_logger.Info($"Adding {package.PackageVersion.Name} {package.PackageVersion.Version} (distributor: {package.PackageVersion.DistributorName})");

Expand Down
19 changes: 19 additions & 0 deletions TwinpackTests/ConfigFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ public async Task CreateFromSolutionFileWithoutFilterAsync()
Assert.AreEqual(@"Tc3_Module=*", references[2]);
}

[DataRow(true)]
[DataRow(false)]
[DataTestMethod]
public async Task CreateFromSolution_DirectoryNotFound_Async(bool continueWithoutSolution)
{
await Assert.ThrowsExceptionAsync<DirectoryNotFoundException>(async () => await ConfigFactory.CreateFromSolutionFileAsync(@"assets\NoSuchDirectory", continueWithoutSolution));
}

[DataRow(true)]
[DataRow(false)]
[DataTestMethod]
public async Task CreateFromSolution_NoFiles_Async(bool continueWithoutSolution)
{
if (!Directory.Exists(@"assets\NoSolutionInside"))
Directory.CreateDirectory(@"assets\NoSolutionInside");

Assert.IsNull(await ConfigFactory.CreateFromSolutionFileAsync(@"assets\NoSolutionInside", continueWithoutSolution));
}

[TestMethod]
public async Task CreateFromSolutionFileWithFilterAsync()
{
Expand Down
1 change: 0 additions & 1 deletion TwinpackTests/SystemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public static void SetUp(TestContext context)
}
}


public class SystemTest
{
protected static Config _config;
Expand Down
18 changes: 13 additions & 5 deletions TwinpackTests/TwinpackServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ public async Task DownloadPackageVersionAsync_DownloadProvidedPackages()
Assert.IsTrue(downloadedPackageVersions.Any(x => x.PackageVersion.Name == "ExternalLib3"));
}

private TwinpackService BuildMultiProjectConfig()
private TwinpackService BuildMultiProjectConfig(out Config config)
{
var packageServer = new PackageServerMock
{
Expand Down Expand Up @@ -935,6 +935,14 @@ private TwinpackService BuildMultiProjectConfig()
}
},
new PackageVersionGetResponse()
{
Name = "ZCore", Version = "1.5.0.3", Branch = "main", Configuration = "Release", Target = "TC3.1",
Dependencies = new List<PackageVersionGetResponse>
{
new PackageVersionGetResponse() { Name = "ExternalLib1", Version = "2.2.3.4" },
}
},
new PackageVersionGetResponse()
{
Name = "ExternalLib1", Version = "1.2.3.4", Branch = "main", Configuration = "Release", Target = "TC3.1",
},
Expand All @@ -946,7 +954,7 @@ private TwinpackService BuildMultiProjectConfig()
Connected = true
};

var config = new Config
config = new Config
{
Projects = new List<ConfigProject>
{
Expand Down Expand Up @@ -994,7 +1002,7 @@ private TwinpackService BuildMultiProjectConfig()
[TestMethod]
public async Task RestoreAsync_NoIncludedProvidedPackages_NoIncludedDependencies()
{
var twinpack = BuildMultiProjectConfig();
var twinpack = BuildMultiProjectConfig(out _);

var packages = await twinpack.RestorePackagesAsync(
new TwinpackService.RestorePackageOptions
Expand All @@ -1016,7 +1024,7 @@ public async Task RestoreAsync_NoIncludedProvidedPackages_NoIncludedDependencies
[TestMethod]
public async Task RestoreAsync_IncludedProvidedPackages_NoIncludedDependencies()
{
var twinpack = BuildMultiProjectConfig();
var twinpack = BuildMultiProjectConfig(out _);

var packages = await twinpack.RestorePackagesAsync(
new TwinpackService.RestorePackageOptions
Expand Down Expand Up @@ -1052,7 +1060,7 @@ public async Task RestoreAsync_IncludedProvidedPackages_NoIncludedDependencies()
[TestMethod]
public async Task RestoreAsync_IncludedProvidedPackages_IncludedDependencies()
{
var twinpack = BuildMultiProjectConfig();
var twinpack = BuildMultiProjectConfig(out _);

var packages = await twinpack.RestorePackagesAsync(
new TwinpackService.RestorePackageOptions
Expand Down

0 comments on commit d7eebe7

Please sign in to comment.