Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore non-package folders in package cache #102

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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