Esempio n. 1
0
        public void ExporterWorksWhenFileIsLocked()
        {
            string resultsDirectoryPath = Path.GetTempPath();
            var    exporter             = new MockExporter();
            var    mockSummary          = GetMockSummary(resultsDirectoryPath);
            var    filePath             = $"{Path.Combine(mockSummary.ResultsDirectoryPath, mockSummary.Title)}-report.txt"; // ExporterBase default

            try
            {
                exporter.ExportToFiles(mockSummary, NullLogger.Instance);

                Assert.Equal(1, exporter.ExportCount);
                Assert.True(File.Exists(filePath));
                using (var handle = File.OpenRead(filePath)) // Gets a lock on the target file
                {
                    exporter.ExportToFiles(mockSummary, NullLogger.Instance);
                    Assert.Equal(2, exporter.ExportCount);
                }
                var savedFiles = Directory.EnumerateFiles(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath) + "*");
                Assert.Equal(2, savedFiles.Count());
            }
            finally
            {
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
                var otherFiles = Directory.EnumerateFiles(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath) + "*");
                foreach (var file in otherFiles)
                {
                    File.Delete(file);
                }
            }
        }
        public void ConfigPassingTest()
        {
            var          types        = new[] { typeof(ClassB) };
            var          switcher     = new BenchmarkSwitcher(types);
            var          config       = ManualConfig.CreateEmpty();
            MockExporter mockExporter = new MockExporter();

            config.Add(mockExporter);
            switcher.Run(new[] { "job=Dry", "class=ClassB", "methods=Method4" }, config);

            Assert.True(mockExporter.exported);
        }
Esempio n. 3
0
        public void ConfigPassingTest()
        {
            var          types        = new[] { typeof(ClassB) };
            var          switcher     = new BenchmarkSwitcher(types);
            var          config       = ManualConfig.CreateEmpty();
            MockExporter mockExporter = new MockExporter();

            config.Add(mockExporter);
            switcher.Run(new[] { "--job", "Dry", "--filter", "*ClassB*" }, config);

            Assert.True(mockExporter.exported);
        }
        public void JobNotDefinedButStillBenchmarkIsExecuted()
        {
            var          types                   = new[] { typeof(JustBenchmark) };
            var          switcher                = new BenchmarkSwitcher(types);
            MockExporter mockExporter            = new MockExporter();
            var          configWithoutJobDefined = ManualConfig.CreateEmpty().With(mockExporter);

            var results = switcher.Run(new[] { "--filter", "*" }, configWithoutJobDefined);

            Assert.True(mockExporter.exported);

            Assert.Single(results);
            Assert.Single(results.SelectMany(r => r.BenchmarksCases));
            Assert.Single(results.SelectMany(r => r.BenchmarksCases.Select(bc => bc.Job)));
            Assert.True(results.All(r => r.BenchmarksCases.All(bc => bc.Job == Job.Default)));
        }
        public void WhenJobIsDefinedViaAttributeAndArgumentsDontContainJobArgumentOnlySingleJobIsUsed()
        {
            var          types                   = new[] { typeof(WithDryAttribute) };
            var          switcher                = new BenchmarkSwitcher(types);
            MockExporter mockExporter            = new MockExporter();
            var          configWithoutJobDefined = ManualConfig.CreateEmpty().With(mockExporter);

            var results = switcher.Run(new[] { "--filter", "*WithDryAttribute*" }, configWithoutJobDefined);

            Assert.True(mockExporter.exported);

            Assert.Single(results);
            Assert.Single(results.SelectMany(r => r.BenchmarksCases));
            Assert.Single(results.SelectMany(r => r.BenchmarksCases.Select(bc => bc.Job)));
            Assert.True(results.All(r => r.BenchmarksCases.All(bc => bc.Job == Job.Dry)));
        }
Esempio n. 6
0
        public void ExporterWritesToFile()
        {
            string resultsDirectoryPath = Path.GetTempPath();
            var    exporter             = new MockExporter();
            var    mockSummary          = GetMockSummary(resultsDirectoryPath);
            var    filePath             = $"{Path.Combine(mockSummary.ResultsDirectoryPath, mockSummary.Title)}-report.txt"; // ExporterBase default

            try
            {
                exporter.ExportToFiles(mockSummary, NullLogger.Instance);

                Assert.Equal(1, exporter.ExportCount);
                Assert.True(File.Exists(filePath));
            }
            finally
            {
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
            }
        }
Esempio n. 7
0
        public void ExporterUsesSimpleTypeNameAsFileNameIfTypeNamesDoNotDuplicate()
        {
            string resultsDirectoryPath = Path.GetTempPath();
            var    exporter             = new MockExporter();
            var    mockSummary          = GetMockSummary(resultsDirectoryPath, typeof(ClassA));
            var    expectedFilePath     = $"{Path.Combine(mockSummary.ResultsDirectoryPath, typeof(ClassA).Name)}-report.txt";
            string actualFilePath       = null;

            try
            {
                actualFilePath = exporter.ExportToFiles(mockSummary, NullLogger.Instance).First();

                Assert.Equal(expectedFilePath, actualFilePath);
            }
            finally
            {
                if (File.Exists(actualFilePath))
                {
                    File.Delete(actualFilePath);
                }
            }
        }
Esempio n. 8
0
        public void ExporterUsesSummaryTitleAsFileNameWhenBenchmarksJoinedToSingleSummary()
        {
            string resultsDirectoryPath = Path.GetTempPath();
            var    exporter             = new MockExporter();
            var    mockSummary          = GetMockSummary(resultsDirectoryPath, typeof(ClassA), typeof(ClassB));
            var    expectedFilePath     = $"{Path.Combine(mockSummary.ResultsDirectoryPath, mockSummary.Title)}-report.txt";
            string actualFilePath       = null;

            try
            {
                actualFilePath = exporter.ExportToFiles(mockSummary, NullLogger.Instance).First();

                Assert.Equal(expectedFilePath, actualFilePath);
            }
            finally
            {
                if (File.Exists(actualFilePath))
                {
                    File.Delete(actualFilePath);
                }
            }
        }
Esempio n. 9
0
        public void ExporterUsesFullyQualifiedTypeNameAsFileName()
        {
            string resultsDirectoryPath = Path.GetTempPath();
            var    exporter             = new MockExporter();
            var    mockSummary          = GetMockSummary(resultsDirectoryPath, config: null, typeof(Generic <int>));
            var    expectedFilePath     = $"{Path.Combine(mockSummary.ResultsDirectoryPath, "BenchmarkDotNet.IntegrationTests.Generic_Int32_")}-report.txt";
            string actualFilePath       = null;

            try
            {
                actualFilePath = exporter.ExportToFiles(mockSummary, NullLogger.Instance).First();

                Assert.Equal(expectedFilePath, actualFilePath);
            }
            finally
            {
                if (File.Exists(actualFilePath))
                {
                    File.Delete(actualFilePath);
                }
            }
        }