Exemple #1
0
        public void Test_GetFileSize_Return_Correct_Size()
        {
            // Arrange
            var fileSize               = 100000000;
            var expectedSizeInByte     = $"{fileSize}B";
            var expectedSizeInKiloByte = $"100000";
            var expectedSizeInMB       = $"95.37MB";

            var content  = new string('A', fileSize);
            var stream   = content.ToStream();
            var testFile = new DotNetHelper_IO.FileObject(TestFolder.FullName + new Randomizer().String(8, 'A', 'Z'));


            // Act
            testFile.Write(stream);
            var fileSizeInBytes     = testFile.GetSize(SizeUnits.Byte);
            var fileSizeInKiloBytes = testFile.GetSize(SizeUnits.Kb);
            var fileSizeInMegaBytes = testFile.GetSize(SizeUnits.Mb);

            var fileSizeAsString = testFile.GetSize();

            // Assert
            Assert.That(fileSizeInBytes, Is.EqualTo(fileSize));
            Assert.That(fileSizeInKiloBytes, Is.EqualTo(97656));
            Assert.That(fileSizeInMegaBytes, Is.EqualTo(95));
            Assert.That(fileSizeAsString, Is.EqualTo(expectedSizeInMB));
        }
Exemple #2
0
        public void Test_DisposeShouldNotThrowError()
        {
            var newFile = $"{TestFolder.FullName}DisposeTest";

            using (var file = new DotNetHelper_IO.FileObject(newFile))
            {
            }
            Assert.Pass("Successfully dispose new instance without error");
        }
Exemple #3
0
        public void Test_CreateFileObject_HaveAPathTYpeOfFile_WhenFIleExist()
        {
            var testFile = RandomTestFileNoExtension;


            var filePath = testFile.FilePathOnly + "AnotherTest.Gohan";

            using (var file = new DotNetHelper_IO.FileObject(filePath))
            {
                file.CreateOrTruncate();
                Assert.AreEqual(file.PathType, PathType.File);
            }
        }
Exemple #4
0
        public async Task Test_Copy_To_File_IncrementFileName_WhenFileNameEndsWithNumber()
        {
            var testFile   = RandomTestFileNoExtension;
            var outputFile = new DotNetHelper_IO.FileObject(RandomTestFileNoExtension.FullName + "1");

            await testFile.WriteAsync("ABC");

            outputFile.CreateOrTruncate();

            var newFileName = await testFile.CopyToAsync(outputFile.FullName, FileOption.IncrementFileNameIfExist);

            Assert.That(!newFileName.Equals(testFile.FullName + "1"));
            Assert.That((await File.ReadAllTextAsync(newFileName)).Equals("ABC"));
        }
Exemple #5
0
        public void Test_Change_Extension_With_File_Having_A_Extension()
        {
            var testFile = RandomTestFileNoExtension;


            var filePath = testFile.FilePathOnly + "AnotherTest.Gohan";

            using (var file = new DotNetHelper_IO.FileObject(filePath))
            {
                file.CreateOrTruncate();
                file.ChangeExtension(".Vegeta", FileOption.Overwrite);
                FileShouldExist($"{file.FilePathOnly}{file.FileNameOnlyNoExtension}.Vegeta");
                FileShouldNotExist(filePath);
                Assert.Pass("Successfully change extension of file");
            }
        }
Exemple #6
0
        /// <summary>
        /// return the FullName of where the file was copied to.
        /// </summary>
        /// <param name="copyToFullName">The file path to copy to .</param>
        /// <param name="option"></param>
        /// <exception cref="T:System.ArgumentOutOfRangeException"></exception>
        /// <exception cref="T:System.ArgumentNullException"></exception>
        /// <exception cref="T:System.UnauthorizedAccessException"> throws if the application doesn't have the required permission </exception>
        public string CopyTo(string copyToFullName, FileOption option)
        {
            copyToFullName.IsNullThrow();

            switch (option)
            {
            case FileOption.Append:
                Directory.CreateDirectory(Path.GetDirectoryName(copyToFullName));
                File.Copy(FullName, copyToFullName, false);
                return(copyToFullName);

            case FileOption.Overwrite:
                Directory.CreateDirectory(Path.GetDirectoryName(copyToFullName));
                File.Copy(FullName, copyToFullName, true);
                return(copyToFullName);

            case FileOption.IncrementFileNameIfExist:
                var incrementedFileName = new FileObject(copyToFullName).GetIncrementFileName();
                Directory.CreateDirectory(Path.GetDirectoryName(incrementedFileName));
                File.Copy(FullName, incrementedFileName, true);
                return(incrementedFileName);

            case FileOption.IncrementFileExtensionIfExist:
                var incrementedFileExtension = new FileObject(copyToFullName).GetIncrementFileName();
                Directory.CreateDirectory(Path.GetDirectoryName(incrementedFileExtension));
                File.Copy(FullName, incrementedFileExtension, true);
                return(incrementedFileExtension);

            case FileOption.DoNothingIfExist:
                if (!File.Exists(copyToFullName))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(copyToFullName));
                    File.Copy(FullName, copyToFullName, true);
                }
                return(copyToFullName);

            case FileOption.ReadOnly:
                throw new Exception("The fileoption read-only isn't valid for a write operation of CopyTo");

            default:
                throw new ArgumentOutOfRangeException(nameof(option), option, null);
            }
        }
Exemple #7
0
        /// <summary>
        /// Copy the current file to the destination with progress
        /// </summary>
        /// <param name="copyToFullName"></param>
        /// <param name="option"></param>
        /// <param name="cancellationToken"></param>
        /// <param name="progress"></param>
        /// <param name="bufferSize"></param>
        /// <returns></returns>
        public async Task <string> CopyToAsync(string copyToFullName, FileOption option, CancellationToken cancellationToken, IProgress <long> progress, int bufferSize = 4096)
        {
#if NETSTANDARD2_1
            await using var sourceStream     = GetFileStream(FileOption.ReadOnly, bufferSize, true).fileStream;
            var(destinationStream, fullName) = new FileObject(copyToFullName).GetFileStream(option, bufferSize, true);
            await using (destinationStream)
            {
                await sourceStream.CopyToAsync(destinationStream, progress, cancellationToken, bufferSize)
                .ConfigureAwait(continueOnCapturedContext: false);
            }
#else
            using var sourceStream           = GetFileStream(FileOption.ReadOnly, bufferSize, true).fileStream;
            var(destinationStream, fullName) = new FileObject(copyToFullName).GetFileStream(option, bufferSize, true);
            using (destinationStream)
            {
                await sourceStream.CopyToAsync(destinationStream, progress, cancellationToken, bufferSize)
                .ConfigureAwait(continueOnCapturedContext: false);
            }
#endif
            return(fullName);
        }
Exemple #8
0
        public void Test_Write_And_Read_Hello_To_And_From_File([Values] FileOption fileOption)
        {
            // Arrange
            var content  = $"Hello";
            var encoding = Encoding.ASCII;
            var testFile = RandomTestFileNoExtension;

            // Act
            // Assert
            if (fileOption == FileOption.ReadOnly)
            {
                // Writing to file is not allow when requesting read-only option
                Assert.That(() => { testFile.Write(content, fileOption, encoding); }, Throws.Exception);
            }
            else
            {
                Assert.That(() =>
                {
                    var file = new DotNetHelper_IO.FileObject(testFile.Write(content, fileOption, encoding));
                    Assert.That(file.ReadAllText(), Is.EqualTo(content));
                }, Throws.Nothing);
            }
        }