chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading;
using NUnit.Framework;
 
namespace OpenTap.Package.UnitTests
{
    [TestFixture]
    public class PackageSlashTests
    {
        private void VerifyContent(string basePath, bool shouldExist)
        {
            var files = new string[]
            {
                Path.Combine(basePath, dir1, filename),
                Path.Combine(basePath, dir1, dir2, filename),
                Path.Combine(basePath, PackageDirRelative, filename)
            };
 
            foreach (var file in files)
            {
                if (shouldExist)
                    Assert.AreEqual(File.ReadAllText(file), sampleText);
                else
                    FileAssert.DoesNotExist(file);
            }
            
            if (shouldExist)
                FileAssert.Exists(Path.Combine(basePath, PackageDirRelative, "package.xml"));
            else
                FileAssert.DoesNotExist(Path.Combine(basePath, PackageDirRelative, "package.xml"));
        }
        
        private const string dir1 = "TestPackageDir";
        private string TapDir => Path.GetDirectoryName(typeof(PluginSearcher).Assembly.Location);
        private const string dir2 = "Subdir";
        private const string name = "PackageName";
        private const string os = "Windows,Linux";
        private string FullName => Path.Combine(dir1, dir2, name).Replace("\\", "/");
        private string PackageDirRelative => Path.Combine("Packages", FullName);
        private string PackageDir => Path.Combine(TapDir, PackageDirRelative);
        string PackageCache => PackageCacheHelper.PackageCacheDirectory;
        private const string version = "3.2.1";
        private const string filename = "SampleFile.txt";
        private const string packageFileName = "TestPackage.xml";
        private const string sampleText = "Sample File Content";
 
        private string Filenameify(string path) => path.Replace('\\', '/').Replace('/', '.');
 
        private string outputPackagePath => Filenameify(Path.Combine(dir1, dir2, $"{name}.{version}.TapPackage"));
                
        private string downloadedPackagePath =>
            Filenameify(Path.Combine(dir1, dir2, $"{name}.{version}.{os}.TapPackage"));
        
        private string description =
            "test package for testing that forward and backwards slashes are handled correctly in package names";
 
        [Test]
        public void SlashTests([Values(true, false)] bool usePackageReference)
        {
            if (Directory.Exists(dir1))
                Directory.Delete(dir1, true);
            
            CreateTestPackage();
            VerifyPackageContent();
            DownloadPackage(usePackageReference);
            InstallPackage();
            VerifyInstalled();
            UninstallPackage();
        }
        public void CreateTestPackage()
        {
            var packageXml = $@"<?xml version=""1.0"" encoding=""UTF-8""?>
<Package Name=""{FullName}"" xmlns=""http://opentap.io/schemas/package"" Version=""{version}"" OS=""{os}"" >
  <Description>
      {description}
  </Description>
  <Files>
      <File Path=""{dir1}/{filename}"" SourcePath=""{filename}""/>
      <File Path=""{dir1}\{dir2}/{filename}"" SourcePath=""{filename}""/>
      <File Path=""{PackageDirRelative}/{filename}"" SourcePath=""{filename}""/>
  </Files>
</Package>
";
            File.WriteAllText(filename, sampleText);
            File.WriteAllText(packageFileName, packageXml);
 
            var create = new PackageCreateAction {Install = false, PackageXmlFile = packageFileName};
            Assert.AreEqual(0, create.Execute(CancellationToken.None));
        }
 
        public void VerifyPackageContent()
        {
            FileAssert.Exists(outputPackagePath);
            var testDir = "__NewTestDir__";
            
            if (Directory.Exists(testDir))
                Directory.Delete(testDir, true);
            
            ZipFile.ExtractToDirectory(outputPackagePath, testDir);
            
            VerifyContent(testDir, true);
        }
        
 
        public void DownloadPackage(bool usePackageReference)
        {
            var outputPath = "DownloadedPackage.TapPackage";
            if (File.Exists(outputPath))
                File.Delete(outputPath);
 
            var download = new PackageDownloadAction()
            {
                ForceInstall = true,
                Repository = new[] { Directory.GetCurrentDirectory() },
                NoCache = true,
                OutputPath = outputPath
            };
 
            if (usePackageReference)
            {
                download.PackageReferences = new[] { new PackageSpecifier(FullName, VersionSpecifier.Any) };
            }
            else
            {
                download.Packages = new[] { FullName };
            }
 
            Assert.AreEqual(0, download.Execute(CancellationToken.None));
            FileAssert.Exists(outputPath);
            FileAssert.Exists(Path.Combine(PackageCache, outputPath));
 
            download = new PackageDownloadAction()
            {
                ForceInstall = true,
                Repository = new[] { Directory.GetCurrentDirectory() },
                Packages = new[] {FullName}
            };
 
            outputPath = downloadedPackagePath;
                        
            if (File.Exists(outputPath))
                File.Delete(outputPath);
            
            Assert.AreEqual(0, download.Execute(CancellationToken.None));
 
            FileAssert.Exists(outputPath);
            FileAssert.Exists(Path.Combine(PackageCache, outputPath));
        }
 
        public void InstallPackage()
        {
            if (Directory.Exists(dir1))
                Directory.Delete(dir1, true);
            
            {  // Install test
                var packageXml = Path.Combine(PackageDir, "package.xml");
 
                var install = new PackageInstallAction()
                {
                    Force = true,
                    NonInteractive = true,
                    Repository = new[] { Directory.GetCurrentDirectory() },
                    Packages = new[] {FullName}
                };
 
                if (File.Exists(packageXml))
                    File.Delete(packageXml);
 
                FileAssert.DoesNotExist(packageXml);
                Assert.AreEqual(0, install.Execute(CancellationToken.None));
                FileAssert.Exists(packageXml);
 
                VerifyContent("", true);
            }
        }
 
        public void VerifyInstalled()
        {
            var installed = new Installation(ExecutorClient.ExeDir).GetPackages();
            Assert.IsTrue(installed.Any(i => i.Name == FullName), $"Expected {FullName} to be installed");
        }
 
        public void UninstallPackage()
        {
            var uninstall = new PackageUninstallAction()
            {
                Force = true,
                NonInteractive = true,
                Packages = new[] {FullName}
            };
 
            DirectoryAssert.Exists(dir1);
            VerifyContent("", true);
 
            Assert.AreEqual(0, uninstall.Execute(CancellationToken.None));
 
            DirectoryAssert.DoesNotExist(dir1);
            VerifyContent("", false);
        }
    }
}