private UnpkgCatalog SetupCatalog(ICacheService cacheService = null, INpmPackageSearch packageSearch = null, INpmPackageInfoFactory infoFactory = null, Dictionary <string, string> prepopulateFiles = null)
        {
            string cacheFolder = Environment.ExpandEnvironmentVariables(@"%localappdata%\Microsoft\Library\");

            if (prepopulateFiles != null)
            {
                foreach (KeyValuePair <string, string> item in prepopulateFiles)
                {
                    // put the provider IdText into the path to mimic the provider implementation
                    string filePath      = Path.Combine(cacheFolder, UnpkgProvider.IdText, item.Key);
                    string directoryPath = Path.GetDirectoryName(filePath);
                    Directory.CreateDirectory(directoryPath);
                    File.WriteAllText(filePath, item.Value);
                    _prepopulatedFiles.Add(filePath);
                }
            }

            IWebRequestHandler defaultRequestHandler = new Mocks.WebRequestHandler();

            return(new UnpkgCatalog(UnpkgProvider.IdText,
                                    new VersionedLibraryNamingScheme(),
                                    new Logger(),
                                    infoFactory ?? new NpmPackageInfoFactory(defaultRequestHandler),
                                    packageSearch ?? new NpmPackageSearch(defaultRequestHandler),
                                    cacheService ?? new CacheService(defaultRequestHandler),
                                    Path.Combine(cacheFolder, UnpkgProvider.IdText)));
        }
        public static Mocks.WebRequestHandler SetupFiles(this Mocks.WebRequestHandler h, string libraryId, string githubLibraryId)
        {
            string files = @"{ ""files"": [ { ""name"": ""testFile.js"" } ] }";

            return(h.ArrangeResponse(string.Format(JsDelivrCatalog.LibraryFileListUrlFormat, libraryId), files)
                   .ArrangeResponse(string.Format(JsDelivrCatalog.LibraryFileListUrlFormatGH, githubLibraryId), files));
        }
Beispiel #3
0
        public static Mocks.WebRequestHandler SetupVersions(this Mocks.WebRequestHandler h, string libraryName)
        {
            string packageData = @"{
  ""version"": ""1.0.0""
}";

            return(h.ArrangeResponse(string.Format(UnpkgCatalog.LatestLibraryVersonUrl, libraryName), packageData));
        }
        [Ignore] // TODO: GetLatestVersion currently only looks for the stable tag.
        public async Task GetLatestVersion_PreRelease()
        {
            const string libraryId = "[email protected]";

            Mocks.WebRequestHandler fakeHandler = new Mocks.WebRequestHandler().SetupVersions("fakeLib", "fake/fakeLib");
            JsDelivrCatalog         sut         = SetupCatalog(fakeHandler);

            string result = await sut.GetLatestVersion(libraryId, true, CancellationToken.None);

            Assert.AreEqual("2.0.0-beta", result);
        }
Beispiel #5
0
        public async Task GetLibraryAsync_Success()
        {
            Mocks.WebRequestHandler handler = new Mocks.WebRequestHandler().SetupFiles("[email protected]");
            UnpkgCatalog            sut     = SetupCatalog(handler);

            ILibrary library = await sut.GetLibraryAsync("fakeLib", "1.0.0", CancellationToken.None);

            Assert.IsNotNull(library);
            Assert.AreEqual("fakeLib", library.Name);
            Assert.AreEqual("1.0.0", library.Version);
        }
Beispiel #6
0
        [Ignore] // TODO: GetLatestVersion currently doesn't distinguish stable and pre-release versions
        public async Task GetLatestVersion_PreRelease()
        {
            const string libraryName = "fakeLibrary";

            Mocks.WebRequestHandler handler = new Mocks.WebRequestHandler().SetupVersions("fakeLibrary");
            UnpkgCatalog            sut     = SetupCatalog(handler);

            string result = await sut.GetLatestVersion(libraryName, true, CancellationToken.None);

            Assert.AreEqual("2.0.0-beta", result);
        }
        public static Mocks.WebRequestHandler SetupVersions(this Mocks.WebRequestHandler h, string libraryId, string githubLibraryId)
        {
            string versions = @"{
  ""tags"": {
    ""beta"": ""2.0.0-beta"",
    ""latest"": ""1.0.0""
  }
}";

            return(h.ArrangeResponse(string.Format(JsDelivrCatalog.LatestLibraryVersionUrl, libraryId), versions)
                   .ArrangeResponse(string.Format(JsDelivrCatalog.LatestLibraryVersionUrlGH, githubLibraryId), versions));
        }
Beispiel #8
0
        public static Mocks.WebRequestHandler SetupFiles(this Mocks.WebRequestHandler h, string libraryId)
        {
            string files = @"{
  ""type"": ""directory"",
  ""files"": [
    {
      ""path"": ""testFile.js"",
      ""type"": ""file""
    }
  ]
}";

            (string name, string version) = new VersionedLibraryNamingScheme().GetLibraryNameAndVersion(libraryId);

            return(h.ArrangeResponse(string.Format(UnpkgCatalog.LibraryFileListUrlFormat, name, version), files));
        }
        public async Task GetLibraryAsync_Success()
        {
            Mocks.WebRequestHandler fakeRequestHandler = new Mocks.WebRequestHandler().SetupFiles("[email protected]", "jquery/[email protected]");
            JsDelivrCatalog         sut = SetupCatalog(fakeRequestHandler);

            CancellationToken token   = CancellationToken.None;
            ILibrary          library = await sut.GetLibraryAsync("jquery", "3.3.1", token);

            Assert.IsNotNull(library);
            Assert.AreEqual("jquery", library.Name);
            Assert.AreEqual("3.3.1", library.Version);

            ILibrary libraryGH = await sut.GetLibraryAsync("jquery/jquery", "3.3.1", token);

            Assert.IsNotNull(libraryGH);
            Assert.AreEqual("jquery/jquery", libraryGH.Name);
            Assert.AreEqual("3.3.1", libraryGH.Version);
        }
Beispiel #10
0
        public async Task NpmPackageSearch_GetPackageInfoAsync_ScopedPackage()
        {
            string searchItem       = "@angular/cli";
            var    expectedVersions = (new[] { "1.0.1", "2.1.7", "3.1.4-pi" })
                                      .Select(x => SemanticVersion.Parse(x))
                                      .ToList();
            string packageInfoRequest = "https://registry.npmjs.org/@angular%2fcli";
            var    requestHandler     = new Mocks.WebRequestHandler();

            requestHandler.ArrangeResponse(packageInfoRequest, FakeResponses.FakeLibraryWithVersions);
            var sut = new NpmPackageInfoFactory(requestHandler);

            NpmPackageInfo packageInfo = await sut.GetPackageInfoAsync(searchItem, CancellationToken.None);

            Assert.AreEqual("fakelibrary", packageInfo.Name);
            Assert.AreEqual("fake description", packageInfo.Description);
            CollectionAssert.AreEquivalent(expectedVersions, packageInfo.Versions.ToList());
        }
        public async Task GetLatestVersion_LatestExist()
        {
            const string libraryId = "[email protected]";

            Mocks.WebRequestHandler fakeHandler = new Mocks.WebRequestHandler().SetupVersions("fakeLib", "fake/fakeLib");
            JsDelivrCatalog         sut         = SetupCatalog(fakeHandler);

            string result = await sut.GetLatestVersion(libraryId, false, CancellationToken.None);

            Assert.AreEqual("1.0.0", result);

            // TODO: A new test should be added for when the tags are missing.  This is passing because the fake
            //       data is as expected.  However, real data does not include the tags and would return null.
            //       The original test implementation checked for null and didn't assert anything in that case.
            const string libraryIdGH = "fake/[email protected]";
            string       resultGH    = await sut.GetLatestVersion(libraryIdGH, false, CancellationToken.None);

            Assert.AreEqual("1.0.0", resultGH);
        }
Beispiel #12
0
        public void Setup()
        {
            string cacheFolder = Environment.ExpandEnvironmentVariables(@"%localappdata%\Microsoft\Library\");

            _projectFolder = Path.Combine(Path.GetTempPath(), "LibraryManager");

            var hostInteraction = new HostInteraction(_projectFolder, cacheFolder);

            var requestHandler     = new Mocks.WebRequestHandler();
            var npmPackageSearch   = new NpmPackageSearch(requestHandler);
            var packageInfoFactory = new NpmPackageInfoFactory(requestHandler);

            var dependencies = new Dependencies(hostInteraction, new JsDelivrProviderFactory(npmPackageSearch, packageInfoFactory));

            _provider = dependencies.GetProvider("jsdelivr");

            LibraryIdToNameAndVersionConverter.Instance.Reinitialize(dependencies);
            Directory.CreateDirectory(_projectFolder);
        }
Beispiel #13
0
        public void AsyncWriteAndReadToCacheFileDoesNotThrow()
        {
            // Arrange
            List <Thread>           threads     = new List <Thread>();
            Random                  rnd         = new Random();
            CancellationTokenSource tokenSource = new CancellationTokenSource();
            CancellationToken       token       = tokenSource.Token;

            Mocks.WebRequestHandler _requestHandler = new Mocks.WebRequestHandler();

            Debug.WriteLine("Starting test");

            for (int i = 0; i < 10; i++)
            {
                Thread thread = new Thread(async delegate()
                {
                    for (int j = 0; j < 100; j++)
                    {
                        int next = rnd.Next(1, 100);

                        if (next % 2 == 0)
                        {
                            Stream content  = await _requestHandler.GetStreamAsync("FakeUrl", token);
                            string fileName = string.Format(_cacheFilePath, rnd.Next(1, 100));
                            await FileHelpers.WriteToFileAsync(fileName, content, token);
                            Debug.WriteLine(string.Format("Thread: {0} => Wrote to file {1}", i, fileName));
                        }
                        else
                        {
                            string fileName = string.Format(_cacheFilePath, rnd.Next(1, 100));
                            if (File.Exists(fileName))
                            {
                                await FileHelpers.ReadFileTextAsync(fileName, token);
                                Debug.WriteLine(string.Format("Thread: {0} => Read from file {1}", i, fileName));
                            }
                        }
                    }

                    for (int j = 0; j < 100; j++)
                    {
                        int next = rnd.Next(1, 100);

                        if (next % 2 == 0)
                        {
                            Stream content  = await _requestHandler.GetStreamAsync("FakeUrl", token);
                            string fileName = string.Format(_projectFilePath, rnd.Next(1, 100));
                            await FileHelpers.WriteToFileAsync(fileName, content, token);
                            Debug.WriteLine(string.Format("Thread: {0} => Wrote to file {1}", i, fileName));
                        }
                        else
                        {
                            string fileName = string.Format(_projectFilePath, rnd.Next(1, 100));
                            await FileHelpers.ReadFileTextAsync(fileName, token);
                            Debug.WriteLine(string.Format("Thread: {0} => Read from file {1}", i, fileName));
                        }
                    }
                });

                threads.Add(thread);
                thread.Start();
            }

            for (int i = 0; i < threads.Count; i++)
            {
                threads[i].Join();
            }

            //Verify
            Assert.IsTrue(true);
        }