public void GetOptions_returns_expected_omit_xml_declaration_value(bool value)
        {
            // Arrange
              var input = new CommandLineOptions() {
            OmitXmlDeclarations = value
              };

              // Act
              var result = _sut.GetOptions(input);

              // Assert
              Assert.AreEqual(value, result.OmitXmlDeclaration);
        }
        public void GetOptions_returns_expected_context_visitor_type_names()
        {
            // Arrange
              var value = _autofixture.Create<string>();
              var input = new CommandLineOptions() {
            ContextVisitorClassNames = value
              };

              // Act
              var result = _sut.GetOptions(input);

              // Assert
              Assert.AreEqual(value, result.ContextVisitorTypes);
        }
        public void GetOptions_returns_expected_output_encoding()
        {
            // Arrange
              var value = _autofixture.Create<string>();
              var input = new CommandLineOptions() {
            OutputEncoding = value
              };

              // Act
              var result = _sut.GetOptions(input);

              // Assert
              Assert.AreEqual(value, result.OutputEncodingName);
        }
        private void AddKeywordOptions(CommandLineOptions options, ref IRenderingOptions renderingOptions)
        {
            if(!String.IsNullOrEmpty(options.KeywordOptions))
              {
            var keywordOptions = options.KeywordOptions
              .Split(KEYWORD_OPTION_SEPARATOR)
              .Select(GetKeywordOption);

            foreach(var option in keywordOptions)
            {
              renderingOptions.KeywordOptions.Add(option.Item1, option.Item2);
            }
              }
        }
        public void GetOptions_returns_expected_keyword_options()
        {
            // Arrange

              var input = new CommandLineOptions() {
            KeywordOptions = "foo=bar;wibble=wobble;spork=splife"
              };

              // Act
              var result = _sut.GetOptions(input);

              // Assert
              Assert.AreEqual(3, result.KeywordOptions.Count, "Count of keyword options");
              Assert.AreEqual("bar",    result.KeywordOptions["foo"],     "First option as expected");
              Assert.AreEqual("wobble", result.KeywordOptions["wibble"],  "Second option as expected");
              Assert.AreEqual("splife", result.KeywordOptions["spork"],   "Third option as expected");
        }
        public IBatchRenderingOptions GetBatchOptions(CommandLineOptions options)
        {
            var inputFiles = options.InputPaths.Select(GetInputFile).ToArray();
              var useStdin = ReadFromStandardInput(inputFiles);
              var ignoredPaths = GetIgnoredPaths(options);

              var outputPath = GetOutputPath(options);
              var useStdOut = OutputToStdOut(inputFiles, outputPath);

              return new BatchRenderingOptions(inputStream: useStdin? Console.OpenStandardInput() : null,
                                       outputStream: useStdOut? Console.OpenStandardOutput() : null,
                                       inputPaths: inputFiles.Where(x => x != null),
                                       outputPath: outputPath,
                                       inputSearchPattern: options.InputFilenamePattern,
                                       outputExtensionOverride: options.OutputFilenameExtension,
                                       ignoredPaths: ignoredPaths,
                                       renderingMode: options.GetRenderingMode());
        }
        public IRenderingOptions GetOptions(CommandLineOptions options)
        {
            if(options == null)
              {
            throw new ArgumentNullException(nameof(options));
              }

              IRenderingOptions output = new RenderingOptions() {
            AddSourceFileAnnotation = options.EnableSourceAnnotation,
            OmitXmlDeclaration = options.OmitXmlDeclarations,
            ContextVisitorTypes = options.ContextVisitorClassNames,
            RenderingContextFactoryType = options.RenderingContextFactoryClassName,
            OutputEncodingName = options.OutputEncoding
              };

              AddKeywordOptions(options, ref output);

              return output;
        }
        private IEnumerable<DirectoryInfo> GetIgnoredPaths(CommandLineOptions options)
        {
            IEnumerable<DirectoryInfo> output;

              if(String.IsNullOrEmpty(options.IgnoredPaths))
              {
            output = new DirectoryInfo[0];
              }
              else
              {
            output = options.IgnoredPaths
              .Split(IGNORED_PATH_SEPARATOR)
              .Select(x => {
            var absolutePath = MakeAbsolutePath(x);
            return Directory.Exists(absolutePath)? new DirectoryInfo(absolutePath) : null;
              });
              }

              return output;
        }
示例#9
0
    public void WritesErrorToStdErrAndTerminates_WhenInvalidEncodingSpecified()
    {
      // Arrange
      var optionsFactory = new Mock<IRenderingOptionsFactory>();
      optionsFactory
        .Setup(x => x.GetOptions(It.IsAny<CommandLineOptions>()))
        .Throws<InvalidOutputEncodingException>();

      var sut = new Application(renderer: _renderer.Object,
                                renderingOptionsFactory: optionsFactory.Object,
                                terminator: _terminator.Object);
      var options = new CommandLineOptions();

      // Act
      var errorOutput = ExerciseSutWithStdErrRedirection(sut, options);

      // Assert
      string expected = @"ERROR: The encoding specified by the --output-encoding argument is invalid.
The encoding (where specified) must be valid encoding 'WebName'.  Consult the manual for more information.
";
      Assert.AreEqual(expected, errorOutput, "Correct message written");
      _terminator.Verify(x => x.Terminate(ApplicationTerminator.ExpectedErrorExitCode),
                         Times.Once(),
                         "Application should be terminated after writing message");
    }
        public void Setup()
        {
            _autofixture = new Fixture();
              _baseDirectory = new DirectoryInfo(System.Environment.CurrentDirectory);
              _sut = new BatchRenderingOptionsFactory(_baseDirectory);

              _options = new CommandLineOptions();
        }
示例#11
0
    public void WritesErrorToStdErrAndTerminates_WhenBothHtmlAndXmlModesSpecified()
    {
      // Arrange
      var sut = new Application(renderer: _renderer.Object,
                                terminator: _terminator.Object);
      var options = new CommandLineOptions() {
        ForceHtmlMode = true,
        ForceXmlMode = true,
      };

      // Act
      var errorOutput = ExerciseSutWithStdErrRedirection(sut, options);

      // Assert
      string expected = @"ERROR: The '--html' and '--xml' arguments are mutually exclusive and may not be used together.
Use 'ZptBuilder.exe --help' for help on providing correct arguments, or consult the manual.
";
      Assert.AreEqual(expected, errorOutput, "Correct message written");
      _terminator.Verify(x => x.Terminate(ApplicationTerminator.ExpectedErrorExitCode),
                         Times.Once(),
                         "Application should be terminated after writing message");
    }
示例#12
0
    private string ExerciseSutWithStdErrRedirection(Application sut,
                                                    CommandLineOptions options)
    {
      string errorOutput;

      using(var writer = SetupStdErrRedirection())
      {
        sut.Begin(options);

        errorOutput = writer.ToString();
      }

      return errorOutput;
    }
示例#13
0
    public void WritesErrorToStdErrAndTerminates_WhenTooManyInputsSpecified()
    {
      // Arrange
      var sut = new Application(renderer: _renderer.Object,
                                terminator: _terminator.Object);
      var options = new CommandLineOptions();

      _renderer
        .Setup(x => x.Render(It.IsAny<IBatchRenderingOptions>(),
                             It.IsAny<IRenderingOptions>()))
        .Throws(new InvalidBatchRenderingOptionsException(_autofixture.Create<string>(),
                                                          BatchRenderingFatalErrorType.InputCannotBeBothStreamAndPaths));

      // Act
      var errorOutput = ExerciseSutWithStdErrRedirection(sut, options);

      // Assert
      string expected = @"ERROR: The inputs must either be a list of paths OR '-' (indicating the use of standard
input),  not both.  Use 'ZptBuilder.exe --help', or consult the manual.
";

      try
      {
        Assert.That(errorOutput.StartsWith(expected), "Correct message written (only the start of the message)");
      }
      catch(AssertionException)
      {
        _logger.Error(errorOutput);
        throw;
      }
      _terminator.Verify(x => x.Terminate(ApplicationTerminator.ExpectedErrorExitCode),
                         Times.Once(),
                         "Application should be terminated after writing message");
    }
示例#14
0
    public void WritesErrorToStdErrAndTerminates_WhenUnexpectedErrorOccursDuringRendering()
    {
      // Arrange
      var sut = new Application(renderer: _renderer.Object,
                                terminator: _terminator.Object);
      var options = new CommandLineOptions();

      _renderer
        .Setup(x => x.Render(It.IsAny<IBatchRenderingOptions>(),
                             It.IsAny<IRenderingOptions>()))
        .Throws<InvalidOperationException>();

      // Act
      var errorOutput = ExerciseSutWithStdErrRedirection(sut, options);

      // Assert
      string expected = @"ERROR: The application has encountered a fatal unexpected error; please report this as a bug at
https://github.com/csf-dev/ZPT-Sharp

Please include the information below with your bug report
---
";

      try
      {
        Assert.That(errorOutput.StartsWith(expected), "Correct message written (only the start of the message)");
      }
      catch(AssertionException)
      {
        _logger.Error(errorOutput);
        throw;
      }
      _terminator.Verify(x => x.Terminate(ApplicationTerminator.UnexpectedErrorExitCode),
                         Times.Once(),
                         "Application should be terminated after writing message");
    }
        public void GetOptions_returns_expected_rendering_context_factory_type_names()
        {
            // Arrange
              var value = _autofixture.Create<string>();
              var input = new CommandLineOptions() {
            RenderingContextFactoryClassName = value
              };

              // Act
              var result = _sut.GetOptions(input);

              // Assert
              Assert.AreEqual(value, result.RenderingContextFactoryType);
        }
示例#16
0
    public void WritesErrorToStdErrAndTerminates_WhenInvalidInputFilesAreSpecified()
    {
      // Arrange
      var filename = "FOO BAR";

      var batchFactory = new Mock<IBatchRenderingOptionsFactory>();
      batchFactory
        .Setup(x => x.GetBatchOptions(It.IsAny<CommandLineOptions>()))
        .Throws(new InvalidInputPathException() { Path = filename });

      var sut = new Application(renderer: _renderer.Object,
                                batchOptionsFactory: batchFactory.Object,
                                terminator: _terminator.Object);
      var options = new CommandLineOptions();

      // Act
      var errorOutput = ExerciseSutWithStdErrRedirection(sut, options);

      // Assert
      string expected = @"ERROR: A specified input path is invalid, perhaps it does not exist or you have insufficient permissions to access it?
The problematic path is:FOO BAR
";
      Assert.AreEqual(expected, errorOutput, "Correct message written");
      _terminator.Verify(x => x.Terminate(ApplicationTerminator.ExpectedErrorExitCode),
                         Times.Once(),
                         "Application should be terminated after writing message");
    }
示例#17
0
    public void WritesErrorToStdErrAndTerminates_WhenInvalidKeywordOptionSpecified()
    {
      // Arrange
      var optionname = "FOO BAR";
      var optionsFactory = new Mock<IRenderingOptionsFactory>();
      optionsFactory
        .Setup(x => x.GetOptions(It.IsAny<CommandLineOptions>()))
        .Throws(new InvalidKeywordOptionsException() { InvalidOption = optionname });

      var sut = new Application(renderer: _renderer.Object,
                                renderingOptionsFactory: optionsFactory.Object,
                                terminator: _terminator.Object);
      var options = new CommandLineOptions();

      // Act
      var errorOutput = ExerciseSutWithStdErrRedirection(sut, options);

      // Assert
      string expected = @"ERROR: An option specified using the --keyword-options argument is in an invalid format.
Where specified, options must be in the format 'OPTION1=VALUE1;OPTION2=VALUE2' and so on.
The invalid option is:FOO BAR
";
      Assert.AreEqual(expected, errorOutput, "Correct message written");
      _terminator.Verify(x => x.Terminate(ApplicationTerminator.ExpectedErrorExitCode),
                         Times.Once(),
                         "Application should be terminated after writing message");
    }
示例#18
0
    public void WritesErrorToStdErrAndTerminates_WhenInvalidRenderingContextFactorySpecified()
    {
      // Arrange
      var optionsFactory = new Mock<IRenderingOptionsFactory>();
      optionsFactory
        .Setup(x => x.GetOptions(It.IsAny<CommandLineOptions>()))
        .Throws(new CouldNotCreateRenderingContextFactoryException() { InvalidClassname = _autofixture.Create<string>() });

      var sut = new Application(renderer: _renderer.Object,
                                renderingOptionsFactory: optionsFactory.Object,
                                terminator: _terminator.Object);
      var options = new CommandLineOptions();

      // Act
      var errorOutput = ExerciseSutWithStdErrRedirection(sut, options);

      // Assert
      string expected = @"ERROR: The type named using the --rendering-context-factory argument could not be instantiated.
Where specified, this type must exist and have a parameterless constructor.
";
      Assert.AreEqual(expected, errorOutput, "Correct message written");
      _terminator.Verify(x => x.Terminate(ApplicationTerminator.ExpectedErrorExitCode),
                         Times.Once(),
                         "Application should be terminated after writing message");
    }
示例#19
0
    public void WritesErrorToStdErrAndTerminates_WhenInvalidOutputPathSpecified()
    {
      // Arrange
      var batchFactory = new Mock<IBatchRenderingOptionsFactory>();
      batchFactory
        .Setup(x => x.GetBatchOptions(It.IsAny<CommandLineOptions>()))
        .Throws(new InvalidOutputPathException());

      var sut = new Application(renderer: _renderer.Object,
                                batchOptionsFactory: batchFactory.Object,
                                terminator: _terminator.Object);
      var options = new CommandLineOptions();

      // Act
      var errorOutput = ExerciseSutWithStdErrRedirection(sut, options);

      // Assert
      string expected = @"ERROR: The specified output path is invalid. Where a directory path is specified, the directory must exist.
Where a file path is specified, the file's parent directory must exist.  Please consult the manual for further guidance.
";
      Assert.AreEqual(expected, errorOutput, "Correct message written");
      _terminator.Verify(x => x.Terminate(ApplicationTerminator.ExpectedErrorExitCode),
                         Times.Once(),
                         "Application should be terminated after writing message");
    }
        private FileSystemInfo GetOutputPath(CommandLineOptions options)
        {
            FileSystemInfo output;

              if(String.IsNullOrEmpty(options.OutputPath))
              {
            output = null;
              }
              else
              {
            var absolutePath = MakeAbsolutePath(options.OutputPath);
            if(Directory.Exists(absolutePath))
            {
              output = new DirectoryInfo(absolutePath);
            }
            else
            {
              FileInfo file;
              try
              {
            file = new FileInfo(absolutePath);
              }
              catch(Exception ex)
              {
            throw new InvalidOutputPathException(ExceptionMessages.InvalidOutputFile, ex);
              }

              if(file.Directory.Exists)
              {
            output = file;
              }
              else
              {
            throw new InvalidOutputPathException(ExceptionMessages.InvalidOutputFile);
              }
            }
              }

              return output;
        }
        public void GetOptions_returns_expected_source_annotation_value(bool value)
        {
            // Arrange
              var input = new CommandLineOptions() {
            EnableSourceAnnotation = value
              };

              // Act
              var result = _sut.GetOptions(input);

              // Assert
              Assert.AreEqual(value, result.AddSourceFileAnnotation);
        }