Skip to content

Commit

Permalink
Merge pull request #102 from FirelyTeam/bugfix/71-packagereference-ca…
Browse files Browse the repository at this point in the history
…nnot-deal-with-non-package-folders

Ignore non-package folders in package cache
  • Loading branch information
mmsmits authored Feb 21, 2024
2 parents b4b172d + eaca77b commit 7900fbf
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
50 changes: 50 additions & 0 deletions Firely.Fhir.Packages.Tests/DiskPackageCacheTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Linq;

namespace Firely.Fhir.Packages.Tests
{
[TestClass]
public class DiskPackageCacheTests
{
[TestMethod]
public void TestIgnoreInvalidPackageFolder()
{

//Create new packageCache folder:
// --testcache
// ---- validpackage#1.0.0
// ---- invalidpackage
// ---- #invalidpackage
// ---- invalidpackage#

string root = "testCache";
string validPackage = "validpackage#2.0.0";
string invalidPackage1 = "invalidpackage";
string invalidPackage2 = "#invalidpackage";
string invalidPackage3 = "invalidpackage#";
string invalidPackage4 = "invalidpackage#2.0.0#invalid";


Directory.CreateDirectory(root);
Directory.CreateDirectory($"{root}/{validPackage}");
Directory.CreateDirectory($"{root}/{invalidPackage1}");
Directory.CreateDirectory($"{root}/{invalidPackage2}");
Directory.CreateDirectory($"{root}/{invalidPackage3}");
Directory.CreateDirectory($"{root}/{invalidPackage4}");

//Test
var packageCache = new DiskPackageCache(root);
var packages = packageCache.GetPackageReferences().Result;
packages.Should().OnlyContain(x => x.Name == validPackage.Split('#', System.StringSplitOptions.None).First());


//Cleanup
Directory.Delete(root, true);


}

}
}
14 changes: 12 additions & 2 deletions Firely.Fhir.Packages/Disk/DiskPackageCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Firely.Fhir.Packages
Expand Down Expand Up @@ -106,13 +107,22 @@ public Task<IEnumerable<PackageReference>> GetPackageReferences()
{
var name = Disk.GetFolderName(folder);

var reference = parseFoldernameToReference(name);
references.Add(reference);
if (isValidPackageFolder(name))
{
var reference = parseFoldernameToReference(name);
references.Add(reference);
}
}

return Task.FromResult(references.AsEnumerable());
}

//check whether the folder name has contains a single # sign which is not at the beginning or the end.
string pattern = @"^[^#]+#[^#]+$";

private bool isValidPackageFolder(string folderName) => Regex.IsMatch(folderName, pattern);


private static PackageReference parseFoldernameToReference(string foldername)
{
var idx = foldername.IndexOf('#');
Expand Down

0 comments on commit 7900fbf

Please sign in to comment.