public void LineVsBranchCodeCoverageTest()
        {
            CodeCoverage cc = new CodeCoverage();

            cc.LineVsBranchCoverage(true);
            //cc.LineVsBranchCoverage(false);
        }
Exemplo n.º 2
0
        public void SQL2005NotSupported()
        {
            var coverage = new CodeCoverage(TestServer2005ConnectionString, TestDatabaseName);

            coverage.Start();
            Assert.That(coverage.IsStarted == false);
            Assert.That(coverage.Exception is SqlCoverException);
        }
Exemplo n.º 3
0
        public void Can_Get_All_Batches()
        {
            var coverage = new CodeCoverage(ConnectionStringReader.GetIntegration(), TestDatabaseName);
            var results  = coverage.Cover("select 1");

            Assert.IsNotNull(results);

            Console.WriteLine(results.RawXml());
        }
Exemplo n.º 4
0
        public void Logging_Works()
        {
            var debugListener = new DebugListener();

            Debug.Listeners.Add(debugListener);
            var coverage = new CodeCoverage(TestServerConnectionString, TestDatabaseName, null, true);
            var results  = coverage.Cover("SELECT 1");

            Assert.True(debugListener.Messages.Count > 0);
        }
Exemplo n.º 5
0
 private void ReportCodeCoverage(TestServiceProvider testService)
 {
     if (CodeCoverage.HitBlockCount > 0 && testService != null && testService.HasService(TestServiceFeature.CodeCoverageReporting))
     {
         CodeCoverageProvider ccp = testService.GetService <CodeCoverageProvider>(TestServiceFeature.CodeCoverageReporting);
         if (ccp != null)
         {
             string data = CodeCoverage.GetCoverageData();
             ccp.SaveCoverageData(data, /* no callback Action */ null);
         }
     }
 }
Exemplo n.º 6
0
        public void TestBestFile()
        {
            var file1 = "C:\\Some\\Path\\file1.py";
            var file2 = "C:\\Some\\Path\\file2.py";
            var best  = CodeCoverage.UpdateBestFile(null, file1);

            Assert.AreEqual(best, file1);

            best = CodeCoverage.UpdateBestFile(null, file1);
            Assert.AreEqual(best, file1);

            best = CodeCoverage.UpdateBestFile(best, file2);
            Assert.AreEqual("C:\\Some\\Path", best);
        }
        public void Doesnt_Die_When_Finding_Encrypted_Stored_Procedures()
        {
            var databaseGateway = new DatabaseGateway(ConnectionStringReader.GetIntegration(), TestDatabaseName);

            databaseGateway.Execute(@"if not exists (select * from sys.procedures where name = 'enc')
begin
	exec sp_executesql N'create procedure enc with encryption 
	as
	select 100;'
end", 15);

            var coverage = new CodeCoverage(ConnectionStringReader.GetIntegration(), TestDatabaseName, null, true, false);
            var results  = coverage.Cover("exec enc");
            //if we dont die we are good
        }
Exemplo n.º 8
0
        private void RunTestCases(
            IEnumerable <TestCase> tests,
            IRunContext runContext,
            IFrameworkHandle frameworkHandle
            )
        {
            bool   codeCoverage = CodeCoverage.EnableCodeCoverage(runContext);
            string covPath      = null;

            if (codeCoverage)
            {
                covPath = CodeCoverage.GetCoveragePath(tests);
            }
            // .py file path -> project settings
            var sourceToSettings = RunSettingsUtil.GetSourceToProjSettings(runContext.RunSettings, filterType: TestFrameworkType.UnitTest);

            foreach (var testGroup in tests.GroupBy(x => sourceToSettings[x.CodeFilePath]))
            {
                if (_cancelRequested.WaitOne(0))
                {
                    break;
                }

                if (testGroup.Key.TestFramework != TestFrameworkType.UnitTest)
                {
                    continue;
                }

                using (var runner = new TestRunner(
                           frameworkHandle,
                           runContext,
                           testGroup,
                           covPath,
                           testGroup.Key,
                           _app,
                           _cancelRequested
                           )) {
                    runner.Run();
                }
            }

            if (codeCoverage)
            {
                CodeCoverage.AttachCoverageResults(frameworkHandle, covPath);
            }
        }
Exemplo n.º 9
0
        public void TimeoutCalling_Cover_Fails_With_Timeout_Exception()
        {
            var coverage = new CodeCoverage(ConnectionStringReader.GetIntegration(), TestDatabaseName, new[] { ".*tSQLt.*", ".*proc.*" });

            try
            {
                coverage.Cover("WAITFOR DELAY '1:00:00'", 1);
            }
            catch (System.Data.SqlClient.SqlException e)
            {
                if (e.Number == -2)
                {
                    return;
                }
            }

            Assert.Fail("expected sql exception with -2 number");
        }
Exemplo n.º 10
0
        public void Does_Not_Cover_Views()
        {
            var coverage = new CodeCoverage(ConnectionStringReader.GetIntegration(), TestDatabaseName);

            coverage.Start();
            using (var con = new SqlConnection(ConnectionStringReader.GetIntegration()))
            {
                con.Open();
                using (var cmd = con.CreateCommand())
                {
                    cmd.CommandText = "select * from a_view";
                    cmd.ExecuteNonQuery();
                }
            }

            var result = coverage.Stop();

            Assert.IsFalse(result.Html().Contains("a_view"));
        }
Exemplo n.º 11
0
        public void Code_Coverage_Covers_Last_Statement()
        {
            var coverage = new CodeCoverage(ConnectionStringReader.GetIntegration(), TestDatabaseName);

            coverage.Start();
            using (var con = new SqlConnection(ConnectionStringReader.GetIntegration()))
            {
                con.Open();
                using (var cmd = con.CreateCommand())
                {
                    cmd.CommandText = "exec [dbo].[set_statements]";
                    cmd.ExecuteNonQuery();
                }
            }

            var result = coverage.Stop();

            Assert.AreEqual(5, result.CoveredStatementCount);   //not sure why SET QUOTED_IDENTIFIER ON is not covered - don't get xevent on 2008
        }
Exemplo n.º 12
0
        public void TimeoutCalling_Cover_Fails_With_Timeout_Exception()
        {
            var coverage = new CodeCoverage(TestServerConnectionString, TestDatabaseName,
                                            new[] { ".*tSQLt.*", ".*proc.*" }, false, false, TraceControllerType.Default, 1);

            try
            {
                coverage.Cover("WAITFOR DELAY '1:00:00'");
            }
            catch (SqlException e)
            {
                if (e.Number == -2)
                {
                    return;
                }
            }

            Assert.Fail("expected sql exception with -2 number");
        }
Exemplo n.º 13
0
        public void Code_Coverage_Returns_All_Covered_Statements()
        {
            var coverage = new CodeCoverage(TestServerConnectionString, TestDatabaseName);

            coverage.Start();
            using (var con = new SqlConnection(TestServerConnectionString))
            {
                con.Open();
                using (var cmd = con.CreateCommand())
                {
                    cmd.CommandText = "exec [dbo].[a_procedure]";
                    cmd.ExecuteNonQuery();
                }
            }

            var result = coverage.Stop();

            Assert.IsTrue(result.RawXml().Contains("HitCount=\"1\""));
        }
Exemplo n.º 14
0
        public void Code_Coverage_Returns_All_Covered_Statements()
        {
            var coverage = new CodeCoverage(ConnectionStringReader.GetIntegration(), TestDatabaseName);

            coverage.Start();
            using (var con = new SqlConnection(ConnectionStringReader.GetIntegration()))
            {
                con.Open();
                using (var cmd = con.CreateCommand())
                {
                    cmd.CommandText = "exec [dbo].[a_procedure]";
                    cmd.ExecuteNonQuery();
                }
            }

            var result = coverage.Stop();

            Assert.That(result.RawXml(), Is.StringContaining("HitCount=\"1\""));
        }
Exemplo n.º 15
0
        public void Code_Coverage_Filters_Statements()
        {
            var coverage = new CodeCoverage(ConnectionStringReader.GetIntegration(), TestDatabaseName, new[] { ".*tSQLt.*", ".*proc.*" });

            coverage.Start();
            using (var con = new SqlConnection(ConnectionStringReader.GetIntegration()))
            {
                con.Open();
                using (var cmd = con.CreateCommand())
                {
                    cmd.CommandText = "exec [dbo].[a_procedure]";
                    cmd.ExecuteNonQuery();
                }
            }

            var result = coverage.Stop();

            Assert.IsFalse(result.RawXml().Contains("HitCount=\"1\""));
            Assert.IsFalse(result.RawXml().Contains("a_procedure"));
        }
Exemplo n.º 16
0
        //  [Ignore("Not sure why failing. Feedback from GoEddie needed.")]
        public void Code_Coverage_Includes_Last_Statement_Of_Large_Procedure()
        {
            var coverage = new CodeCoverage(ConnectionStringReader.GetIntegration(), TestDatabaseName);

            coverage.Start();
            using (var con = new SqlConnection(ConnectionStringReader.GetIntegration()))
            {
                con.Open();
                using (var cmd = con.CreateCommand())
                {
                    cmd.CommandText = "exec [dbo].[a_large_procedure] 1, 1";
                    cmd.ExecuteNonQuery();
                }
            }

            var result = coverage.Stop();

            Assert.That(result.CoveredStatementCount, Is.EqualTo(2));

            var xml = result.OpenCoverXml();
        }
Exemplo n.º 17
0
        public void Code_Coverage_Includes_Last_Statement_Of_Large_Procedure()
        {
            var coverage = new CodeCoverage(TestServerConnectionString, TestDatabaseName);

            coverage.Start();
            using (var con = new SqlConnection(TestServerConnectionString))
            {
                con.Open();
                using (var cmd = con.CreateCommand())
                {
                    cmd.CommandText = "exec [dbo].[a_large_procedure] 1, 1";
                    cmd.ExecuteNonQuery();
                }
            }

            var result = coverage.Stop();

            Assert.IsTrue(result.CoveredStatementCount == 2);

            var xml = result.OpenCoverXml();
        }
Exemplo n.º 18
0
        public void Code_Coverage_Excludes_Label_From_Lines_That_Can_Be_Covered()
        {
            var coverage = new CodeCoverage(ConnectionStringReader.GetIntegration(), TestDatabaseName);

            coverage.Start();
            using (var con = new SqlConnection(ConnectionStringReader.GetIntegration()))
            {
                con.Open();
                using (var cmd = con.CreateCommand())
                {
                    cmd.CommandText = "exec [dbo].[a_procedure_with_goto]";
                    cmd.ExecuteNonQuery();
                }
            }

            var result = coverage.Stop();


            Assert.That(result.CoveredStatementCount, Is.EqualTo(4));
            Assert.That(result.StatementCount, Is.EqualTo(24));
            var xml = result.OpenCoverXml();
        }
        public void Doesnt_Die_When_Finding_Encrypted_Stored_Procedures()
        {
            var databaseGateway = new DatabaseGateway(TestServerConnectionString, TestDatabaseName, 15);

            databaseGateway.Execute(@"if not exists (select * from sys.procedures where name = 'enc')
begin
	exec sp_executesql N'create procedure enc with encryption 
	as
	select 100;'
end");
            var source  = new DatabaseSourceGateway(databaseGateway);
            var batches = source.GetBatches(null);

            foreach (var batch in batches)
            {
                Console.WriteLine("batch: {0}", batch.Text);
            }

            Assert.AreEqual(5, batches.Count());

            var coverage = new CodeCoverage(TestServerConnectionString, TestDatabaseName, null, true, false);

            Assert.DoesNotThrow(() => coverage.Cover("exec enc"));
        }
Exemplo n.º 20
0
        /// <summary>
        /// Initializes the configuration.
        /// </summary>
        /// <param name="database">The database to use as a key-value store for the chain, if none is provided, a new one is created.</param>
        /// <param name="version">An optional version we can supply to override the version numbers derived from our current block number, and the starting block number for a new version.</param>
        /// <param name="genesisBlockHeader">An optional genesis block header to use instead of the default. Expected to have no transactions or uncles.</param>
        /// <param name="genesisState">An optional genesis state to use instead of the default. If this is provided, the genesis block's state root hash will be replaced with this state's root hash, and the database argument should be this genesis state's database, so lookups will resolve to any modified genesis state trie objects.</param>
        public Configuration(BaseDB database = null, EthereumRelease?version = null, BlockHeader genesisBlockHeader = null, State genesisState = null)
        {
            // Set our database, or create one if we don't have a provided one.
            Database = database ?? new BaseDB();

            // Initialize our debug items
            DebugConfiguration = new DebugConfiguration();
            CodeCoverage       = new CodeCoverage();

            // We set our timestamp offset as zero
            CurrentTimestampOffset = TimeSpan.Zero;

            #region Versioning
            // Create our block number lookup.
            EthereumReleaseBlockStart = new Dictionary <EthereumRelease, BigInteger>();

            // If our version to operate under is non-null, then we set it.
            if (version != null)
            {
                SetEthereumReleaseBlockStarts((EthereumRelease)version);
            }
            else
            {
                // Otherwise we initialize with default parameters.
                EthereumReleaseBlockStart[EthereumRelease.Frontier]           = 0; // This is actually 1, but to avoid future logic errors, we'll include the genesis block.
                EthereumReleaseBlockStart[EthereumRelease.Homestead]          = 1150000;
                EthereumReleaseBlockStart[EthereumRelease.DAO]                = 1920000;
                EthereumReleaseBlockStart[EthereumRelease.TangerineWhistle]   = 2463000;
                EthereumReleaseBlockStart[EthereumRelease.SpuriousDragon]     = 2675000;
                EthereumReleaseBlockStart[EthereumRelease.Byzantium]          = 4370000;
                EthereumReleaseBlockStart[EthereumRelease.WIP_Constantinople] = EVMDefinitions.UINT256_MAX_VALUE; // Work in progress
                EthereumReleaseBlockStart[EthereumRelease.WIP_Serenity]       = EVMDefinitions.UINT256_MAX_VALUE; // Work in progress
            }
            #endregion

            #region Genesis Block/State
            // Set up our genesis state if we don't have one.
            if (genesisState == null)
            {
                // We create a genesis state from this state.
                genesisState = new State(this);
            }
            else
            {
                // We override our given genesis state configuration with this one.
                genesisState.Configuration = this;
            }

            // Set up our genesis block header if we don't have one.
            if (genesisBlockHeader == null)
            {
                genesisBlockHeader = new BlockHeader(
                    new byte[KeccakHash.HASH_SIZE],                                        // previous hash
                    new byte[KeccakHash.HASH_SIZE],                                        // uncles hash (recalculated later)
                    new Address(0),                                                        // coinbase
                    Trie.BLANK_NODE_HASH,                                                  // state root hash (taken from our genesis state)
                    Trie.BLANK_NODE_HASH,                                                  // transaction root hash
                    Trie.BLANK_NODE_HASH,                                                  // receipts root hash
                    0,                                                                     // bloom
                    1145132613,                                                            // difficulty
                    0,                                                                     // block number
                    1296127060,                                                            // gas limit
                    0,                                                                     // gas used
                    709953967,                                                             // timestamp
                    "506F6B6F72612F4C6974746C652028486F73686F2047726F757029".HexToBytes(), // extra data
                    new byte[KeccakHash.HASH_SIZE],                                        // mix hash
                    new byte[KeccakHash.HASH_SIZE]);                                       // nonce
            }

            // We set our genesis state trie root hash for our genesis block header
            genesisBlockHeader.StateRootHash = genesisState.Trie.GetRootNodeHash();

            // Initialize our genesis block
            GenesisBlock = new Block(genesisBlockHeader, Array.Empty <Transaction>(), Array.Empty <BlockHeader>());
            GenesisBlock.Header.UpdateUnclesHash(GenesisBlock.Uncles);

            // Set our genesis state snapshot
            GenesisStateSnapshot = genesisState.Snapshot();
            #endregion

            // Accounts
            InitialAccountNonce = 0;

            // Blocks
            PreviousHashDepth = 256;

            // Uncles
            MaxUncles               = 2;
            MaxUncleDepth           = 6;
            UncleDepthPenaltyFactor = 8;

            // Block Rewards
            BlockReward           = 5000 * BigInteger.Pow(10, 15);
            NephewReward          = 5000 * BigInteger.Pow(10, 15) / 32;
            BlockRewardByzantium  = 3000 * BigInteger.Pow(10, 15);
            NephewRewardByzantium = 3000 * BigInteger.Pow(10, 15) / 32;

            // Block Difficulty
            MinDifficulty                             = 131072;
            DifficultyFactor                          = 2048;
            DifficultyAdjustmentCutOff                = 13;
            DifficultyAdjustmentCutOffByzantium       = 9;
            DifficultyAdjustmentCutOffHomestead       = 10;
            DifficultyExponentialPeriod               = 100000;
            DifficultyExponentialFreePeriods          = 2;
            DifficultyExponentialFreePeriodsByzantium = 30;

            // Gas limits
            MinGasLimit = 5000;
            MaxGasLimit = BigInteger.Pow(2, 63) - 1;
            GasLimitExponentialMovingAverageFactor = 1024;
            GasLimitAdjustmentMaxFactor            = 1024;

            GasLimitFactorNumerator   = 3;
            GasLimitFactorDenominator = 2;

            // By default we want to validate ethash.
            IgnoreEthashVerification = false;

            // Set our extra data for our dao fork blocks.
            DAOForkBlockExtraData = Encoding.UTF8.GetBytes("dao-hard-fork");
        }
 public void Doesnt_Die_When_Table_Trigger_Code()
 {
     var coverage = new CodeCoverage(ConnectionStringReader.GetIntegration(), TestDatabaseName, null, true, false);
     var results  = coverage.Cover("insert into a_table select 100");
     //if we dont die we are good
 }
 public void Doesnt_Die_When_Database_Trigger_Code()
 {
     var coverage = new CodeCoverage(ConnectionStringReader.GetIntegration(), TestDatabaseName, null, true, false);
     var results  = coverage.Cover("create synonym abd for a_table");
     //if we dont die we are good
 }
Exemplo n.º 23
0
        /// <summary>
        /// should minic arguments from example\SQLCover.ps1
        /// run by `dotnet run -- -c Get-CoverTSql -r`
        /// `dotnet run -- -c Get-CoverTSql -e Export-OpenXml -k "Data Source=localhost;Initial Catalog=master;User ID=sa;Password=yourStrong(!)Password" -d DatabaseWithTests -q "tSQLt.runAll" -p TestCoverageOpenXml`
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Parser.Default.ParseArguments <Options>(args)
            .WithParsed <Options>(o =>
            {
                if (o.Verbose)
                {
                    Console.WriteLine($"Verbose output enabled. Current Arguments: -v {o.Verbose}");
                    Console.WriteLine("Current Arguments Serialized::" + JsonConvert.SerializeObject(o));
                    Console.WriteLine("SqlCoverCore! App is in Verbose mode!");
                }
                else
                {
                    Console.WriteLine($"Current Arguments: -v {o.Verbose} -c {o.Command}");
                    Console.WriteLine(":::Running SqlCoverCore:::");
                }
                var cType = CommandType.Unknown;
                var eType = CommandType.Unknown;
                string[] requiredParameters       = null;
                string[] requiredExportParameters = null;
                switch (o.Command)
                {
                case "Get-CoverTSql":
                    cType = CommandType.GetCoverTSql;
                    requiredParameters = new string[] {
                        "connectionString",
                        "databaseName",
                        "query"
                    };
                    break;

                case "Get-CoverExe":
                    cType = CommandType.GetCoverExe;
                    requiredParameters = new string[] {
                        "connectionString",
                        "databaseName",
                        "exeName",
                        "args"
                    };
                    break;

                case "Get-CoverRedgateCITest":
                    cType = CommandType.GetCoverRedgateCITest;
                    requiredParameters = new string[] {
                        "outputPath"
                    };
                    break;

                default:
                    Console.WriteLine("Command:" + o.Command + " is not supported");
                    break;
                }

                switch (o.ExportCommand)
                {
                case "Export-OpenXml":
                    eType = CommandType.ExportOpenXml;
                    requiredExportParameters = new string[] {
                        "outputPath"
                    };
                    break;

                case "Start-ReportGenerator":
                    eType = CommandType.StartReportGenerator;
                    requiredExportParameters = new string[] { "outputPath",
                                                              "reportGeneratorPath" };
                    break;

                case "Export-Html":
                    eType = CommandType.ExportHtml;
                    requiredExportParameters = new string[] {
                        "outputPath"
                    };
                    break;

                default:
                    Console.WriteLine("ExportCommand:" + o.ExportCommand + " is not supported");
                    break;
                }

                var validParams = eType != CommandType.Unknown && cType != CommandType.Unknown ? validateRequired(o, requiredParameters) : false;
                validParams     = validParams ? validateRequired(o, requiredExportParameters) : validParams;

                if (validParams)
                {
                    if (o.GetRequiredParameters)
                    {
                        Console.WriteLine(o.Command + " requiredParameters are:" + string.Join(',', requiredParameters));
                        Console.WriteLine(o.ExportCommand + " requiredParameters are:" + string.Join(',', requiredExportParameters));
                    }
                    else
                    {
                        Console.WriteLine(":::Running command" + cType.ToString() + ":::");
                        CodeCoverage coverage  = null;
                        CoverageResult results = null;
                        // run command
                        switch (cType)
                        {
                        case CommandType.GetCoverTSql:
                            coverage = new CodeCoverage(o.ConnectionString, o.databaseName, null, true, o.Debug);
                            results  = coverage.Cover(o.Query);
                            break;

                        case CommandType.GetCoverExe:
                            coverage = new CodeCoverage(o.ConnectionString, o.databaseName, null, true, o.Debug);
                            results  = coverage.CoverExe(o.ExeName, o.Args);
                            break;

                        case CommandType.GetCoverRedgateCITest:
                            //    coverage = new CodeCoverage(o.ConnectionString, o.databaseName, null, true, o.Debug);
                            //    coverage.Start();
                            // run Redgate SQLRelease Module

                            //    results = coverage.Stop();
                            Console.WriteLine(cType.ToString() + " is not YET supported");
                            break;
                        }
                        if (coverage != null && results != null)
                        {
                            Console.WriteLine(":::Running exportCommand" + eType.ToString() + ":::");
                            var resultString = "";
                            var outputPath   = "";
                            if (!string.IsNullOrWhiteSpace(o.OutputPath))
                            {
                                outputPath = o.OutputPath;
                                if (outputPath.Substring(outputPath.Length - 1, 1) != Path.DirectorySeparatorChar.ToString())
                                {
                                    outputPath += Path.DirectorySeparatorChar;
                                }
                            }
                            else
                            {
                                outputPath = "Coverage" + Path.DirectorySeparatorChar;
                                if (!Directory.Exists(outputPath))
                                {
                                    Directory.CreateDirectory(outputPath);
                                }
                            }
                            switch (eType)
                            {
                            case CommandType.ExportOpenXml:
                                resultString = results.OpenCoverXml();
                                results.SaveResult(outputPath + "Coverage.opencover.xml", resultString);
                                results.SaveSourceFiles(outputPath);
                                break;

                            case CommandType.ExportHtml:
                                resultString = results.Html();
                                results.SaveResult(outputPath + "Coverage.html", resultString);
                                results.SaveSourceFiles(outputPath);
                                break;

                            // thinking this should be separate from program, called directly from ci/cd
                            case CommandType.StartReportGenerator:
                                Console.WriteLine(eType.ToString() + " is not YET supported");
                                break;
                            }
                        }
                    }
                }
            });
        }