Exemplo n.º 1
0
        public async Task LoadRulesShouldOnlyReturnPackagesWhenOnlyGivenPackages()
        {
            // Arrange
            var config = new ConfigBuilder()
                         .WithPackage("package1", "1.2")
                         .WithPackage("package2", "0.3")
                         .ToString();

            using var file = new TempFile();
            await File.WriteAllTextAsync(file, config);

            var rulesFactory = Substitute.For <IRuleFactory>();

            rulesFactory.Construct(Arg.Any <PackageReferenceRuleData>()).Returns(Substitute.For <IRule>());
            var output = Substitute.For <IOutput>();
            var sut    = new RulesLoader(rulesFactory, output);

            // Act
            var actual = await sut.LoadRules(file).ToListAsync();

            // Assert
            Assert.Equal(2, actual.Count);
            rulesFactory.Received(2).Construct(Arg.Any <PackageReferenceRuleData>());
            rulesFactory.DidNotReceive().Construct(Arg.Any <PropertyRuleData>());
            rulesFactory.DidNotReceive().Construct(Arg.Any <ProjectReferenceRuleData>());
        }
Exemplo n.º 2
0
        public async Task LoadRulesShouldNotLoadUnconstructableRules()
        {
            // Arrange
            var config = new ConfigBuilder()
                         .WithProperty(new PropertyRuleData())
                         .WithPackage(new PackageReferenceRuleData())
                         .WithProjectReference(new ProjectReferenceRuleData())
                         .ToString();

            using var file = new TempFile();
            await File.WriteAllTextAsync(file, config);

            var rulesFactory = Substitute.For <IRuleFactory>();

            rulesFactory.Construct(Arg.Any <PackageReferenceRuleData>()).Returns((IRule?)null);
            rulesFactory.Construct(Arg.Any <PropertyRuleData>()).Returns((IRule?)null);
            rulesFactory.Construct(Arg.Any <ProjectReferenceRuleData>()).Returns((IRule?)null);
            var output = Substitute.For <IOutput>();
            var sut    = new RulesLoader(rulesFactory, output);

            // Act
            var actual = await sut.LoadRules(file).ToListAsync();

            // Assert
            Assert.Empty(actual);
            rulesFactory.Received(1).Construct(Arg.Any <PropertyRuleData>());
            rulesFactory.Received(1).Construct(Arg.Any <PackageReferenceRuleData>());
            rulesFactory.Received(1).Construct(Arg.Any <ProjectReferenceRuleData>());
        }
Exemplo n.º 3
0
        public async Task LoadRulesShouldBeEmptyForEmptyFile()
        {
            // Arrange
            using var file = new TempFile();
            var rulesFactory = Substitute.For <IRuleFactory>();
            var output       = Substitute.For <IOutput>();
            var sut          = new RulesLoader(rulesFactory, output);

            // Act
            var actual = await sut.LoadRules(file).ToListAsync();

            // Assert
            Assert.Empty(actual);
        }
Exemplo n.º 4
0
        public async Task LoadRulesShouldNotThrowWhenFileDoesNotExist()
        {
            // Arrange
            var rulesFactory = Substitute.For <IRuleFactory>();
            var output       = Substitute.For <IOutput>();
            var sut          = new RulesLoader(rulesFactory, output);

            // Act
            var ex = await Record.ExceptionAsync(async() =>
                                                 await sut.LoadRules(new FileInfo("non.existing")).ToListAsync());

            // Assert
            Assert.Null(ex);
            output.ReceivedWithAnyArgs(2).Error(null !);
        }
Exemplo n.º 5
0
        private static Task <int> Main(string[] args)
        {
            var rootCommand = new RootCommand
            {
                new Option <FileInfo>("--config", "Path to a configuration that defines how validation will occur.")
                {
                    IsRequired = true,
                    AllowMultipleArgumentsPerToken = false,
                    Argument = new Argument <FileInfo>
                    {
                        Arity = ArgumentArity.ExactlyOne
                    }
                }.ExistingOnly(),
                new Option <FileInfo[]>("--project",
                                        "Path to a Visual Studio solution or project files that should be validated. Can be specified multiple times")
                {
                    IsRequired = true,
                    AllowMultipleArgumentsPerToken = true,
                    Argument = new Argument <FileInfo[]>
                    {
                        Arity = ArgumentArity.OneOrMore
                    }
                }.ExistingOnly(),
                new Option <bool>("--verbose", description: "Verbose output of progress", getDefaultValue: () => false),
                new Option <bool>("--silent", description: "Don't output anything on stdout", getDefaultValue: () => false)
            };

            rootCommand.Description = "Validator for Visual Studio projects";
            rootCommand.Handler     = CommandHandler.Create(
                async(FileInfo config, FileInfo[] project, bool verbose, bool silent, IConsole console) =>
            {
                var logger = new Output(console, silent, verbose);

                var rulesFactory = new RuleFactory();
                var rulesLoader  = new RulesLoader(rulesFactory, logger);
                var rules        = await rulesLoader.LoadRules(config).ToListAsync();

                var projectLoader = new ProjectLoader(logger);
                var validator     = new Validator(rules, projectLoader, logger);

                return(await validator.Validate(project));
            });

            return(rootCommand.InvokeAsync(args));
        }