示例#1
0
        private string GetOutputFilePath()
        {
            string composeFileName(string fileNameWithoutExtension) =>
            (_options.Prefix ?? string.Empty) +
            fileNameWithoutExtension +
            (_options.Suffix ?? string.Empty) +
            OutputExtension;

            string fileName = composeFileName(UniqueIdGenerator.UniqueId());
            string basePath = File.Exists(_options.Input)
                ? Path.GetDirectoryName(_options.Input) ?? throw new CombineException($"{_options.Input} parent dir not found, try providing an absolute or relative path")
                : _options.Input !;

            if (_options.Output is not null)
            {
                if (Path.EndsInDirectorySeparator(_options.Output))
                {
                    basePath = _options.Output.ReplaceEndingDirectorySeparatorWithProperEndingDirectorySeparator();
                    Directory.CreateDirectory(basePath);
                }
                else
                {
                    var directoryName = Path.GetDirectoryName(_options.Output);

                    basePath = string.IsNullOrEmpty(directoryName)
                        ? _options.Input + Path.DirectorySeparatorChar
                        : Directory.CreateDirectory(directoryName).FullName;

                    fileName = composeFileName(Path.GetFileNameWithoutExtension(_options.Output));
                }
            }

            return(Path.Combine(basePath, fileName));
        }
        public void NoOutputFileName_GeneratesUniqueFileName(string outputDir)
        {
            // Arrange
            var timeBefore = ParseDateTimeFromFileName(UniqueIdGenerator.UniqueId());

            // Act
            var options = new ZipOptions()
            {
                Output    = outputDir,
                OverWrite = true,
                Input     = InputDir
            };

            var exitCode = new Compressor(options).Run();

            // Assert
            Assert.Equal(0, exitCode);
            var existingFiles = Directory.GetFiles(outputDir);

            var timeAfter = ParseDateTimeFromFileName(UniqueIdGenerator.UniqueId());
            var zipFiles  = existingFiles.Where(f =>
            {
                if (Path.GetExtension(f) == Compressor.OutputExtension)
                {
                    var fileDate = ParseDateTimeFromFileName(Path.GetFileNameWithoutExtension(f));
                    return(fileDate is not null && fileDate >= timeBefore && fileDate <= timeAfter);
                }

                return(false);
            });

            Assert.Single(zipFiles);
        }
示例#3
0
    public async Task NoOutputFileName_GeneratesUniqueFileName(string outputDir)
    {
        // Arrange
        var timeBefore = ParseDateTimeFromFileName(UniqueIdGenerator.UniqueId());

        // Act
        var options = new CombineOptions()
        {
            Output    = outputDir,
            OverWrite = true,
            Input     = InputDir,
            Suffix    = CombinerTestsFixture.DefaultSuffix
        };

        var exitCode = await new Combiner(options).Run();

        // Assert
        Assert.Equal(0, exitCode);
        var existingFiles = Directory.GetFiles(outputDir);

        var timeAfter        = ParseDateTimeFromFileName(UniqueIdGenerator.UniqueId());
        var csGeneratedFiles = existingFiles.Where(f =>
        {
            if (Path.GetExtension(f) == Combiner.OutputExtension)
            {
                var fileDate = ParseDateTimeFromFileName(Path.GetFileNameWithoutExtension(f).Replace(options.Suffix, string.Empty));
                return(fileDate is not null && fileDate >= timeBefore && fileDate <= timeAfter);
            }

            return(false);
        });

        Assert.Single(csGeneratedFiles);
    }