Exemple #1
0
        public async Task Download_GivenUrl_WhenCanDownload_ShouldReturnData()
        {
            //---------------Set up test pack-------------------
            using (var server = new HttpServer())
                using (var tempFile = new AutoTempFile())
                {
                    var relativeUrl = GetRandomString();
                    var expected    = GetRandomBytes();
                    server.AddFileHandler((processor, stream) =>
                    {
                        if (processor.Path != "/" + relativeUrl)
                        {
                            throw new Exception($"Unexpected request {processor.Path}"); // test should fail if incorrect url is used
                        }
                        return(expected);
                    });
                    var sut = Create();

                    //---------------Assert Precondition----------------

                    //---------------Execute Test ----------------------
                    var result = await sut.DownloadDataAsync(server.GetFullUrlFor(relativeUrl));

                    //---------------Test Result -----------------------
                    Assert.IsTrue(result.Success);
                    CollectionAssert.AreEqual(expected, result.Data);
                }
        }
        public void AppendLine_then_Persist_WhenDestinationFolderDoesntExist_ShouldCreateIt()
        {
            //---------------Set up test pack-------------------
            var baseFolder = Path.Combine(Path.GetTempPath(), GetRandomAlphaNumericString(3));
            var lines      = GetRandomCollection <string>(3);

            using (var tempFile = new AutoTempFile(baseFolder, string.Empty))
            {
                File.Delete(tempFile.Path);
                Directory.Delete(baseFolder, true);
                //---------------Assert Precondition----------------
                Assert.IsFalse(Directory.Exists(baseFolder));

                //---------------Execute Test ----------------------
                var sut = Create(tempFile.Path);
                Assert.IsFalse(File.Exists(tempFile.Path));
                lines.ForEach(sut.AppendLine);
                sut.Persist();

                //---------------Test Result -----------------------
                Assert.IsTrue(File.Exists(tempFile.Path));
                var inFile = tempFile.StringData.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
                CollectionAssert.AreEqual(lines, inFile);
            }
        }
        public void LoadNuspecAt_ShouldReturnNewNuspecUtilEveryTimeWithNuspecLoaded()
        {
            //---------------Set up test pack-------------------
            var sut = Create();

            using (var tempFile1 = new AutoTempFile(TestResources.package1))
                using (var tempFile2 = new AutoTempFile(TestResources.package2))
                {
                    //---------------Assert Precondition----------------

                    //---------------Execute Test ----------------------
                    var result1 = sut.LoadNuspecAt(tempFile1.Path);
                    var result2 = sut.LoadNuspecAt(tempFile2.Path);
                    var dup1    = sut.LoadNuspecAt(tempFile1.Path);

                    //---------------Test Result -----------------------
                    Assert.IsInstanceOf <NuspecUtil>(result1);
                    Assert.IsInstanceOf <NuspecUtil>(result2);
                    Assert.IsInstanceOf <NuspecUtil>(dup1);

                    Assert.AreEqual("PeanutButter.TestUtils.Entity", result1.PackageId);
                    Assert.AreEqual("PeanutButter.DatabaseHelpers", result2.PackageId);
                    Assert.AreEqual("PeanutButter.TestUtils.Entity", dup1.PackageId);
                    Assert.AreNotEqual(result1, result2);
                    Assert.AreNotEqual(result1, dup1);
                    Assert.AreNotEqual(result2, dup1);
                }
        }
        public void FindNuspecsUnder_GivenExistingPathWithNuspecsSprinkledUnderThatPath_ShouldAddAllNuspecPaths()
        {
            //---------------Set up test pack-------------------
            var sut   = Create();
            var file1 = RandomValueGen.GetRandomString(5, 10) + ".nuspec";
            var file2 = RandomValueGen.GetRandomString(5, 10) + ".nuspec";

            using (var folder = new AutoTempFolder())
                using (var nuspec1 = new AutoTempFile(folder.Path, file1, TestResources.package1.AsBytes()))
                    using (var nuspec2 = new AutoTempFile(SomeRandomFolderUnder(folder), file2, TestResources.package2.AsBytes()))
                    {
                        //---------------Assert Precondition----------------
                        CollectionAssert.AreEqual(TestResources.package1, nuspec1.BinaryData);
                        CollectionAssert.AreEqual(TestResources.package2, nuspec2.BinaryData);
                        CollectionAssert.IsEmpty(sut.NuspecPaths);

                        //---------------Execute Test ----------------------
                        sut.FindNuspecsUnder(folder.Path);

                        //---------------Test Result -----------------------
                        CollectionAssert.IsNotEmpty(sut.NuspecPaths);
                        CollectionAssert.Contains(sut.NuspecPaths, nuspec1.Path);
                        CollectionAssert.Contains(sut.NuspecPaths, nuspec2.Path);
                    }
        }
        public void Get_GivenSource_WhenCacheFileExists_ShouldReturnContents()
        {
            //---------------Set up test pack-------------------
            var sourceUrl = GetRandomHttpUrl();
            var cacheFilenameGenerator = Substitute.For <ICacheFilenameGenerator>();

            using (var tempFile = new AutoTempFile())
            {
                cacheFilenameGenerator.GenerateFor(sourceUrl).Returns(tempFile.Path);
                var expected = GetRandomCollection <string>(2, 5);
                File.WriteAllBytes(tempFile.Path, expected.JoinWith(Environment.NewLine).AsBytes());
                var sut = Create(cacheFilenameGenerator, new TextFileReaderFactory());

                //---------------Assert Precondition----------------
                CollectionAssert.AreEqual(expected, tempFile.StringData.Split(new[] { Environment.NewLine }, StringSplitOptions.None));

                //---------------Execute Test ----------------------
                var result = sut.GetReaderFor(sourceUrl);

                //---------------Test Result -----------------------
                Assert.IsNotNull(result);
                var resultLines = result.EnumerateLines().ToArray();
                CollectionAssert.AreEqual(expected, resultLines);
            }
        }
Exemple #6
0
        public void ShouldSetDepsForAllFrameworks()
        {
            using (var packageFile = new AutoTempFile(TestResources.multi_target_package))
            {
                // Arrange
                var doc           = XDocument.Parse(packageFile.StringData);
                var startingNodes = doc.XPathSelectElements(
                    "/package/metadata/dependencies/group/dependency"
                    );

                var sut = CreateAndLoad(packageFile.Path);
                Expect(startingNodes).Not.To.Be.Empty();
                Expect(startingNodes.All(
                           n => n.Parent?.Attribute("targetFramework")?.Value == "net40"
                           )).To.Be.True();
                var targetFrameworks = sut.FindTargetedFrameworks();
                Expect(targetFrameworks)
                .To.Be.Equivalent.To(new[] { "net452", "netstandard2.0" });
                // Act
                sut.EnsureSameDependencyGroupForAllTargetFrameworks();
                // Assert
                sut.Persist();

                var after  = XDocument.Parse(packageFile.StringData);
                var groups = after.XPathSelectElements(
                    "/package/metadata/dependencies/group"
                    );
                Expect(groups).To.Contain.Only(2).Items();
                Expect(groups).To.Contain.Exactly(1)
                .Matched.By(n => n.Attribute("targetFramework")?.Value == "net452");
                Expect(groups).To.Contain.Exactly(1)
                .Matched.By(n => n.Attribute("targetFramework")?.Value == "netstandard2.0");
            }
        }
Exemple #7
0
            public void SectionWithKeysOnly_WhenPersisting_ShouldNotAddTrailingEqualitySign()
            {
                //---------------Set up test pack-------------------
                var key1 = GetRandomString();
                var key2 = GetRandomString();
                var src  = new[]
                {
                    "[section]",
                    key1,
                    key2
                };

                //---------------Assert Precondition----------------

                //---------------Execute Test ----------------------
                var sut = Create();

                sut.Parse(src.AsText());
                using (var tempFile = new AutoTempFile())
                {
                    sut.Persist(tempFile.Path);

                    //---------------Test Result -----------------------
                    var outputData = File.ReadAllLines(tempFile.Path);

                    Expect(outputData).To.Equal(new[]
                    {
                        "[section]",
                        key1,
                        key2
                    });
                }
            }
        public void CreateConfigIfNotFound_ShouldCreateConfigWithSettingsSectionWith_(string section, string setting, string expected)
        {
            //---------------Set up test pack-------------------
            var sut             = Create();
            var executingFolder = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
            var fileName        = Constants.CONFIG_FILE;

            using (var tempFile = new AutoTempFile(executingFolder, fileName, new byte[] { }))
            {
                //---------------Assert Precondition----------------
                if (File.Exists(tempFile.Path))
                {
                    File.Delete(tempFile.Path);
                }
                Assert.AreEqual(tempFile.Path, sut.IniFilePath);

                //---------------Execute Test ----------------------
                sut.CreateConfigIfNotFound();


                //---------------Test Result -----------------------
                var iniFile = new INIFile(tempFile.Path);
                Assert.AreEqual(expected, iniFile[section][setting]);
            }
        }
        private AutoTempFile CreateTemporaryDefaultsFile()
        {
            var result = new AutoTempFile();

            DumpDefaultsFileAt($"{result.Path}.cnf");
            return(result);
        }
 public void Persist()
 {
     using (var tempFile = new AutoTempFile())
     {
         WriteLinesWithNoTrailingEmptyLineTo(tempFile.Path);
         EnsureFolderExistsFor(_path);
         File.Copy(tempFile.Path, _path, true);
     }
 }
Exemple #11
0
 public void CanItBeDone()
 {
     // Arrange
     using var file = new AutoTempFile()
           {
               StringData = GetRandomString(8196, 16384)
           };
     // Act
     // Assert
 }
 public void ShouldThrowIfBadFile()
 {
     // Arrange
     using (var tempFile = new AutoTempFile())
     {
         tempFile.StringData = GetRandomString(100);
         // Act
         Expect(() => Create(tempFile.Path))
         .To.Throw <NotSupportedException>();
         // Assert
     }
 }
 public void ShouldThrowForModernProject()
 {
     // Arrange
     using (var tempFile = new AutoTempFile())
     {
         // Act
         tempFile.StringData = MODERN_CSPROJ.Trim();
         // Assert
         Expect(() => Create(tempFile.Path))
         .To.Throw <NotSupportedException>();
     }
 }
 public void ShouldNotThrowForLegacyProject()
 {
     // Arrange
     using (var tempFile = new AutoTempFile())
     {
         tempFile.StringData = LEGACY_CSPROJ.Trim();
         // Act
         Expect(() => Create(tempFile.Path))
         .Not.To.Throw();
         // Assert
     }
 }
        public void ShouldRetrieveSingleUnterminatedStatement()
        {
            // Arrange
            using var file = new AutoTempFile("select * from foo");
            using var sut  = Create(file.Path);
            // Act
            var result = sut.ReadAllStatements();

            // Assert
            Expect(result)
            .To.Equal(new[] { "select * from foo" });
        }
Exemple #16
0
            public void WhenFileExists_ShouldReturnTrue()
            {
                // Arrange
                using var tempFile = new AutoTempFile();
                var sut = Create(Path.GetDirectoryName(tempFile.Path));
                // Act
                var result = sut.Exists(tempFile.Path);

                // Assert
                Expect(result)
                .To.Be.True();
            }
Exemple #17
0
            public void WhenFileExists_RelativeToConstructionPath_ShouldReturnTrue()
            {
                // Arrange
                using var tempFile = new AutoTempFile();
                var container = Path.GetDirectoryName(tempFile.Path);
                var sut       = Create(container);
                // Act
                var result = sut.IsFile(tempFile.Path);

                // Assert
                Expect(result)
                .To.Be.True();
            }
        public void Dispose_ShouldRemoveTempFile()
        {
            //---------------Set up test pack-------------------
            var sut = new AutoTempFile();

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            Assert.IsTrue(File.Exists(sut.Path));
            sut.Dispose();

            //---------------Test Result -----------------------
            Assert.IsFalse(File.Exists(sut.Path));
        }
        public void ShouldExposeFileStringData()
        {
            // Arrange
            var data = GetRandomString(128);

            // Pre-assert
            // Act
            using (var file = new AutoTempFile("stuff.blob", data))
            {
                var result = file.StringData;
                // Assert
                Expect(result).To.Equal(data);
            }
        }
        public void Construct_GivenBasePathAndStringData_ShouldUseThatInsteadOfTempDir()
        {
            //---------------Set up test pack-------------------
            var baseFolder = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            using (var tempFile = new AutoTempFile(baseFolder, ""))
            {
                //---------------Test Result -----------------------
                Assert.AreEqual(baseFolder, Path.GetDirectoryName(tempFile.Path));
            }
        }
        public void Dispose_WhenCalledTwice_ShouldNotBarf()
        {
            //---------------Set up test pack-------------------
            using (var tempFile = new AutoTempFile())
            {
                var sut = Create(tempFile.Path);
                sut.Dispose();
                //---------------Assert Precondition----------------

                //---------------Execute Test ----------------------
                Assert.DoesNotThrow(() => sut.Dispose());

                //---------------Test Result -----------------------
            }
        }
            public void ShouldCreateTheFile()
            {
                // Arrange
                using var tempFile = new AutoTempFile();
                var data = GetRandomBytes(1024);

                using var src = new MemoryStream(data);
                // Act
                src.Save(tempFile.Path);
                // Assert
                var persisted = File.ReadAllBytes(tempFile.Path);

                Expect(persisted)
                .To.Equal(data);
            }
        public void Construct_GivenOneString_SetsContents()
        {
            // Arrange
            var expected = GetRandomString(128);

            // Pre-assert
            // Act
            using (var file = new AutoTempFile(expected))
            {
                // Assert
                Expect(file.StringData).To.Equal(expected);
                Expect(Encoding.UTF8.GetString(File.ReadAllBytes(file.Path)))
                .To.Equal(expected);
            }
        }
        public void ShouldExposeFileBinaryDataForRead()
        {
            // Arrange
            var data = GetRandomBytes(64);

            // Pre-assert
            // Act
            using (var folder = new AutoTempFolder())
                using (var file = new AutoTempFile(folder.Path, data))
                {
                    var result = file.BinaryData;
                    // Assert
                    Expect(result).To.Equal(data);
                }
        }
        public void Construct_GivenBasePath_ShouldUseThatInsteadOfTempDirAndWriteBytes()
        {
            //---------------Set up test pack-------------------
            var baseFolder = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
            var expected   = RandomValueGen.GetRandomBytes();

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            using (var tempFile = new AutoTempFile(baseFolder, expected))
            {
                //---------------Test Result -----------------------
                Assert.AreEqual(baseFolder, Path.GetDirectoryName(tempFile.Path));
                CollectionAssert.AreEqual(expected, File.ReadAllBytes(tempFile.Path));
            }
        }
        public void BinaryData_get_ShouldReturnBytesInFile()
        {
            //---------------Set up test pack-------------------
            var expected = GetRandomBytes();

            using (var sut = new AutoTempFile(expected))
            {
                //---------------Assert Precondition----------------

                //---------------Execute Test ----------------------
                var result = sut.BinaryData;

                //---------------Test Result -----------------------
                Assert.AreEqual(expected, result);
            }
        }
        public void StringData_get_WhenDataInFileIsText_ShouldReturnBytesInFileAsUtf8EncodedString()
        {
            //---------------Set up test pack-------------------
            var expected = GetRandomString();

            using (var sut = new AutoTempFile(expected))
            {
                //---------------Assert Precondition----------------

                //---------------Execute Test ----------------------
                var result = sut.StringData;

                //---------------Test Result -----------------------
                Assert.AreEqual(expected, result);
            }
        }
        public void Construct_GivenBasePathAndStringData_ShouldUseThatInsteadOfTempDirAndWriteBytes()
        {
            //---------------Set up test pack-------------------
            var baseFolder = GetExecutingAssemblyFolder();
            var expected   = GetRandomString();

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            using (var tempFile = new AutoTempFile(baseFolder, expected))
            {
                //---------------Test Result -----------------------
                Assert.AreEqual(baseFolder, Path.GetDirectoryName(tempFile.Path));
                CollectionAssert.AreEqual(expected, File.ReadAllBytes(tempFile.Path));
            }
        }
        public void FileMove_ShouldThrowWhenTargetFileExists()
        {
            // Discovery -- and this is why I don't just use a rename
            //   to get a file atomically moved
            //---------------Set up test pack-------------------
            using (var tempFile1 = new AutoTempFile())
                using (var tempFile2 = new AutoTempFile())
                {
                    //---------------Assert Precondition----------------

                    //---------------Execute Test ----------------------
                    Assert.Throws <IOException>(() => File.Move(tempFile2.Path, tempFile1.Path));

                    //---------------Test Result -----------------------
                }
        }
        public void Construct_GivenSomeBytes_ShouldPutThemInTheTempFile()
        {
            //---------------Set up test pack-------------------
            var expected = GetRandomBytes();

            using (var sut = new AutoTempFile(expected))
            {
                //---------------Assert Precondition----------------

                //---------------Execute Test ----------------------
                var result = File.ReadAllBytes(sut.Path);

                //---------------Test Result -----------------------
                CollectionAssert.AreEqual(expected, result);
            }
        }