示例#1
0
        public void TryToUpdateLicense()
        {
            var model = new LicenseIndexJson
            {
                Code             = "MIT",
                RequiresApproval = true,
                HRef             = "link"
            };

            Assert.ThrowsAsync <NotSupportedException>(() => _sut.CreateLicenseIndexJsonAsync(model, CancellationToken.None));
        }
示例#2
0
        public static async Task CreateLicenseIndexJsonAsync(this IStorage storage, LicenseIndexJson model, CancellationToken token)
        {
            storage.AssertNotNull(nameof(storage));
            model.AssertNotNull(nameof(model));

            var content = JsonSerialize(model);

            try
            {
                await storage.CreateLicenseFileAsync(model.Code, IndexFileName, content, token);
            }
            catch (IOException ex)
            {
                throw new NotSupportedException("License cannot be updated.", ex);
            }
        }
示例#3
0
        private async Task <LicenseIndexJson> LoadLicenseIndexAsync(string code, CancellationToken token)
        {
            if (_indexByCode.TryGetValue(code, out var index))
            {
                return(index);
            }

            index = await Repository.Storage.ReadLicenseIndexJsonAsync(code, token);

            if (index == null)
            {
                Logger.Info("License {0} not found in the repository.".FormatWith(code));
                _indexByCode.Add(code, null);
                return(null);
            }

            var result = new LicenseIndexJson
            {
                FullName = index.FullName.IsNullOrEmpty() ? index.Code : index.FullName,
                HRef     = index.HRef
            };

            _indexByCode.Add(code, result);

            if (!index.FileName.IsNullOrEmpty())
            {
                Directory.CreateDirectory(Path.Combine(To, "Licenses"));
                result.FileName = "Licenses\\{0}-{1}".FormatWith(index.Code, index.FileName);

                using (var content = await Repository.Storage.OpenLicenseFileReadAsync(code, index.FileName, token))
                    using (var dest = new FileStream(Path.Combine(To, result.FileName), FileMode.Create, FileAccess.ReadWrite))
                    {
                        await content.CopyToAsync(dest, token);
                    }
            }

            if (!index.Dependencies.IsNullOrEmpty())
            {
                foreach (var dependency in index.Dependencies)
                {
                    await LoadLicenseIndexAsync(dependency, token);
                }
            }

            return(result);
        }
示例#4
0
        public async Task CreateLoadLicense()
        {
            var model = new LicenseIndexJson
            {
                Code             = "Apache-2.0",
                RequiresApproval = true,
                HRef             = "link",
                FileName         = "file name.md"
            };

            await _sut.CreateLicenseIndexJsonAsync(model, CancellationToken.None);

            var actual = await _sut.ReadLicenseIndexJsonAsync(model.Code, CancellationToken.None);

            actual.ShouldNotBeNull();
            actual.Code.ShouldBe(model.Code);
            actual.RequiresApproval.ShouldBe(model.RequiresApproval);
            actual.HRef.ShouldBe(model.HRef);
            actual.FileName.ShouldBe("file name.md");
        }
示例#5
0
        public async Task <RepositoryLicense> LoadOrCreateLicenseAsync(string licenseCode, CancellationToken token)
        {
            licenseCode.AssertNotNull(nameof(licenseCode));

            var index = await Storage.ReadLicenseIndexJsonAsync(licenseCode, token);

            if (index != null)
            {
                return(new RepositoryLicense(index.Code, index.RequiresApproval, index.RequiresThirdPartyNotices, index.Dependencies));
            }

            var info = await Container.Resolve <ILicenseResolver>().DownloadByCodeAsync(licenseCode, token);

            if (info == null)
            {
                info = new LicenseInfo
                {
                    Code        = licenseCode,
                    FileName    = "license.txt",
                    FileContent = Array.Empty <byte>()
                };
            }

            index = new LicenseIndexJson
            {
                Code                      = info.Code,
                FullName                  = info.FullName,
                HRef                      = info.FileHRef,
                FileName                  = info.FileName,
                RequiresApproval          = true,
                RequiresThirdPartyNotices = false
            };
            await Storage.CreateLicenseIndexJsonAsync(index, token);

            await Storage.CreateLicenseFileAsync(info.Code, info.FileName, info.FileContent, token);

            return(new RepositoryLicense(index.Code, index.RequiresApproval, index.RequiresThirdPartyNotices, index.Dependencies));
        }
示例#6
0
        public void BeforeEachTest()
        {
            _loggerErrors = new List <string>();

            var logger = new Mock <ILogger>(MockBehavior.Strict);

            logger
            .Setup(l => l.Indent())
            .Returns((IDisposable)null);
            logger
            .Setup(l => l.Error(It.IsNotNull <string>()))
            .Callback <string>(message =>
            {
                Console.WriteLine("Error: " + message);
                _loggerErrors.Add(message);
            });

            _storagePackages  = new List <PackageInfo>();
            _sourceReferences = new List <LibraryReference>();

            var container = new UnityContainer();

            _mitLicense = new LicenseIndexJson
            {
                Code                      = "MIT",
                RequiresApproval          = false,
                RequiresThirdPartyNotices = false
            };

            _storage = new Mock <IStorage>(MockBehavior.Strict);
            _storage
            .Setup(s => s.GetAllLibrariesAsync(CancellationToken.None))
            .ReturnsAsync(() => _storagePackages.Select(i => i.Id).ToArray());
            _storage
            .Setup(s => s.OpenLicenseFileReadAsync(_mitLicense.Code, "index.json", CancellationToken.None))
            .ReturnsAsync(_mitLicense.JsonSerialize);

            _packageRepository = new Mock <IPackageRepository>(MockBehavior.Strict);
            _packageRepository
            .SetupGet(r => r.Storage)
            .Returns(_storage.Object);
            _packageRepository
            .Setup(r => r.LoadPackageAsync(It.IsAny <LibraryId>(), CancellationToken.None))
            .Returns <LibraryId, CancellationToken>((id, _) =>
            {
                var package = _storagePackages.Single(i => i.Package.Name == id.Name && i.Package.SourceCode == id.SourceCode && i.Package.Version == id.Version);
                return(Task.FromResult(package.Package));
            });
            _packageRepository
            .Setup(r => r.LoadPackageAsync(It.IsAny <LibraryId>(), CancellationToken.None))
            .Returns <LibraryId, CancellationToken>((id, _) =>
            {
                var package = _storagePackages.Single(i => i.Package.Name == id.Name && i.Package.SourceCode == id.SourceCode && i.Package.Version == id.Version);
                return(Task.FromResult(package.Package));
            });
            container.RegisterInstance(_packageRepository.Object);

            var parser = new Mock <ISourceCodeParser>(MockBehavior.Strict);

            parser
            .Setup(p => p.GetReferences(It.IsNotNull <IList <string> >()))
            .Returns <IList <string> >(locations =>
            {
                locations.ShouldBe(new[] { "source1", "source2" });
                return(_sourceReferences);
            });
            container.RegisterInstance(parser.Object);

            _sut = new ValidateCommand(container, logger.Object)
            {
                Sources = { "source1", "source2" },
                AppName = AppName
            };
        }