示例#1
0
        public void Validate(MainBicepTestFile file)
        {
            var tempFilePath = this.fileSystem.Path.GetTempFileName();

            try
            {
                bicepCliProxy.Build(file.Path, tempFilePath);
            }
            catch (Exception exception)
            {
                this.fileSystem.File.Delete(tempFilePath);

                if (exception is BicepException)
                {
                    // Failed to build the test file. Convert it to InvalidModuleException so that
                    // the validate command can continue validating other files.
                    throw new InvalidModuleException(exception.Message, exception);
                }

                throw;
            }

            this.logger.LogInformation("Making sure the test file contains at least one test...");

            using var tempFileStream = fileSystem.FileStream.CreateDeleteOnCloseStream(tempFilePath);
            var testTemplateElement = JsonElementFactory.CreateElement(tempFileStream);
            var testDeployments     = testTemplateElement.Select($@"$.resources[?(@.type == ""Microsoft.Resources/deployments"" && @.properties.template.metadata._generator.templateHash == ""{this.latestMainArmTemplateFile.TemplateHash}"")]");

            if (testDeployments.IsEmpty())
            {
                throw new InvalidModuleException($"The file \"{file.Path}\" is invalid. Could not find tests in the file. Please make sure to add at least one module referencing the main Bicep file.");
            }
        }
示例#2
0
            protected override int Invoke(InvocationContext context)
            {
                // Read or create main Bicep file.
                this.Logger.LogInformation("Ensuring {MainBicepFile} exists...", "main Bicep file");
                var mainBicepFile = MainBicepFile.EnsureInFileSystem(this.FileSystem);

                // Create main Bicep test file if it doesn't exist.
                this.Logger.LogInformation("Ensuring {MainBicepTestFile} exists...", "main Bicep test file");
                MainBicepTestFile.EnsureInFileSystem(this.FileSystem);

                // Generate main ARM template file.
                var bicepCliProxy       = new BicepCliProxy(this.environmentProxy, this.processProxy, this.FileSystem, this.Logger, context.Console);
                var mainArmTemplateFile = this.GenerateFileAndLogInformation("main ARM template file", () => MainArmTemplateFile
                                                                             .Generate(this.FileSystem, bicepCliProxy, mainBicepFile)
                                                                             .WriteToFileSystem(FileSystem));

                // Read or create metadata file.
                this.Logger.LogInformation("Ensuring {MetadataFile} exists...", "metadata file");
                var metadataFile = MetadataFile.EnsureInFileSystem(this.FileSystem);

                // Generate README file.
                this.GenerateFileAndLogInformation("README file", () => ReadmeFile
                                                   .Generate(this.FileSystem, metadataFile, mainArmTemplateFile)
                                                   .WriteToFileSystem(this.FileSystem));

                // Generate version file.
                this.GenerateFileAndLogInformation("version file", () => VersionFile
                                                   .Generate(this.FileSystem)
                                                   .WriteToFileSystem(this.FileSystem));

                return(0);
            }
示例#3
0
            protected override int Invoke(InvocationContext context)
            {
                var valid = true;

                this.Logger.LogInformation("Validating module path...");
                valid &= Validate(context.Console, () => ValidateModulePath(this.FileSystem));

                this.Logger.LogInformation("Validating main Bicep file...");

                var bicepCliProxy = new BicepCliProxy(this.environmentProxy, this.processProxy, this.FileSystem, this.Logger, context.Console);
                var mainBicepFile = MainBicepFile.ReadFromFileSystem(this.FileSystem);

                // This also validates that the main Bicep file can be built without errors.
                var latestMainArmTemplateFile = MainArmTemplateFile.Generate(this.FileSystem, bicepCliProxy, mainBicepFile);
                var descriptionsValidator     = new DescriptionsValidator(this.Logger, latestMainArmTemplateFile);

                valid &= Validate(context.Console, () => mainBicepFile.ValidatedBy(descriptionsValidator));

                var testValidator       = new TestValidator(this.FileSystem, this.Logger, bicepCliProxy, latestMainArmTemplateFile);
                var jsonSchemaValidator = new JsonSchemaValidator(this.Logger);
                var diffValidator       = new DiffValidator(this.FileSystem, this.Logger, latestMainArmTemplateFile);

                this.Logger.LogInformation("Validating main Bicep test file...");
                valid &= Validate(context.Console, () => MainBicepTestFile.ReadFromFileSystem(this.FileSystem).ValidatedBy(testValidator));

                this.Logger.LogInformation("Validating main ARM template file...");
                valid &= Validate(context.Console, () => MainArmTemplateFile.ReadFromFileSystem(this.FileSystem).ValidatedBy(diffValidator));

                this.Logger.LogInformation("Validating metadata file...");
                valid &= Validate(context.Console, () => MetadataFile.ReadFromFileSystem(this.FileSystem).ValidatedBy(jsonSchemaValidator));

                this.Logger.LogInformation("Validating README file...");
                valid &= Validate(context.Console, () => ReadmeFile.ReadFromFileSystem(this.FileSystem).ValidatedBy(diffValidator));

                this.Logger.LogInformation("Validating version file...");
                valid &= Validate(context.Console, () => VersionFile.ReadFromFileSystem(this.FileSystem).ValidatedBy(jsonSchemaValidator, diffValidator));

                return(valid ? 0 : 1);
            }