Exemplo n.º 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.");
            }
        }
        public static MainArmTemplateFile Generate(IFileSystem fileSystem, BicepCliProxy bicepCliProxy, MainBicepFile mainBicepFile)
        {
            var tempFilePath = fileSystem.Path.GetTempFileName();

            bicepCliProxy.Build(mainBicepFile.Path, tempFilePath);

            using var tempFileStream = fileSystem.FileStream.CreateDeleteOnCloseStream(tempFilePath);
            using var streamReader   = new StreamReader(tempFileStream);

            var path    = fileSystem.Path.GetFullPath(FileName);
            var content = streamReader.ReadToEnd();

            return(new(path, content));
        }