GoEddie / SQLCover

Apache License 2.0
63 stars 52 forks source link

Exception calling "Cover" with "1" argument(s) - also "2" arguments. #44

Open ncbidoug opened 5 years ago

ncbidoug commented 5 years ago

Do you want to request a feature or report a bug? bug or help

What is the current behavior?

I am getting the following when calling Get-CoverTSql. Where is the debug output?

Exception calling "Cover" with "1" argument(s): "Unable to start the trace - errors are recorded in the debug output"
At C:\Users\hoffman\Documents\Apps\SQLCOver.0.4.1\SQLCover.ps1:19 char:2
+     $coverage.Cover($query)
+     ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : SqlCoverException

Also tried using Get-CoverExe and is got an error message from OSQL.EXE indicating that I didn't set up the arg list correctly. The relevant line is commented out below.

Error: No user selected. Try with -U or -E switches
You cannot call a method on a null-valued expression.
At C:\Users\hoffman\Documents\Apps\SQLCOver.0.4.1\SQLCover.ps1:89 char:5
+     $result.OpenCoverXml() | Out-File $xmlPath
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Server version: Microsoft SQL Server 2017 (RTM-CU15) (KB4498951) - 14.0.3162.1 (X64) May 15 2019 19:14:30 Copyright (C) 2017 Microsoft Corporation Standard Edition (64-bit) on Windows Server 2012 R2 Datacenter 6.3 (Build 9600: ) (Hypervisor)

**If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via http://sqlfiddle.com/

# file: test.ps1 
$ErrorActionPreference = "Stop"  

cd c:\Users\hoffman\Documents\Apps\SQLcover

# https://the.agilesql.club/2016/04/sqlcover-code-coverage-for-sql-server-t-sql/
$coverPath='C:\Users\hoffman\Documents\Apps\SQLCOver.0.4.1'
$coverDLL="$coverPath\SQLCover.dll"
$coverPS1="$coverPath\SQLCover.ps1"
# osql and osqlArgs used for Get-CoverExe
$osql='C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\OSQL.EXE'
$server='GTR_DEV16'
$database='GTR'
$SQLuser='gtr_write'
$SQLpass='removed'  # 
$query="EXEC tSQLt.Run 'zut_clinvar_sub_curation.Test_update_sub_date'"
$connectionString = "Persist Security Info=False;User ID=$SQLuser;Password=$SQLpass;Initial Catalog=$database;Server=$server"
$osqlArgs="-S $server -d $database -U $SQLuser -P $SQLpass -i test.sql"
. $coverPS1
$result = Get-CoverTSql "${coverDLL}" "${connectionString}" "${database}" "${query}"
#$result = Get-CoverExe "${coverDLL}" "${connectionString}" "${database}" "${osql}" "${osqlArgs}"
Export-OpenXml $result "testout.xml"

What is the expected behavior? Not receiving an error message

Which versions of SQL Server and which OS are affected by this issue? Did this work in previous versions of our procedures?

Microsoft SQL Server 2017 (RTM-CU15) (KB4498951) - 14.0.3162.1 (X64) May 15 2019 19:14:30 Copyright (C) 2017 Microsoft Corporation Standard Edition (64-bit) on Windows Server 2012 R2 Datacenter 6.3 (Build 9600: ) (Hypervisor)

Client desktop computer: Windows 10 Enterprise Version 1806 (OS Build 17134.1006)

First time attempting to use SQLCover.

GoEddie commented 5 years ago

Hi,

For the debug output run this:

$coverage = new-object SQLCover.CodeCoverage ($connectionString, $databaseName, $null, $true)
$coverage.Cover($query)

so for your script run:

# file: test.ps1 
$ErrorActionPreference = "Stop"  

cd c:\Users\hoffman\Documents\Apps\SQLcover

# https://the.agilesql.club/2016/04/sqlcover-code-coverage-for-sql-server-t-sql/
$coverPath='C:\Users\hoffman\Documents\Apps\SQLCOver.0.4.1'
$coverDLL="$coverPath\SQLCover.dll"
$coverPS1="$coverPath\SQLCover.ps1"
# osql and osqlArgs used for Get-CoverExe
$osql='C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\OSQL.EXE'
$server='GTR_DEV16'
$database='GTR'
$SQLuser='gtr_write'
$SQLpass='removed'  # 
$query="EXEC tSQLt.Run 'zut_clinvar_sub_curation.Test_update_sub_date'"
$connectionString = "Persist Security Info=False;User ID=$SQLuser;Password=$SQLpass;Initial Catalog=$database;Server=$server"
$osqlArgs="-S $server -d $database -U $SQLuser -P $SQLpass -i test.sql"
. $coverPS1
#$result = Get-CoverTSql "${coverDLL}" "${connectionString}" "${database}" "${query}"
#$result = Get-CoverExe "${coverDLL}"
$coverage = new-object SQLCover.CodeCoverage ($connectionString, $database, $null, $true)
$result = $coverage.Cover($query)

Can you tell me the error?

ed

ncbidoug commented 5 years ago

Thank you for your response. I had another error which I reported and have since deleted because it was an unrelated internal problem. Disregard whatever email was generated as a result. I apologize for the inconvenience.

GoEddie commented 5 years ago

Hi,

no problem - can you clarify what error you get now?

There is also a newer release 0.5.0 which has a number of big fixes, probably worth testing with:

https://github.com/GoEddie/SQLCover/releases/tag/0.5.0

ncbidoug commented 5 years ago

I am having permission issues and working with our DBA. Thanks for letting me know about the new version.

ncbidoug commented 5 years ago

I got the original error again with 0.4.1 and 0.5.0 Code:

# https://stackoverflow.com/questions/9948517/how-to-stop-a-powershell-script-on-the-first-error
$ErrorActionPreference = "Stop"  
cd c:\Users\hoffman\Documents\Apps\SQLcover
# https://the.agilesql.club/2016/04/sqlcover-code-coverage-for-sql-server-t-sql/
$coverPath='C:\Users\hoffman\Documents\Apps\SQLCOver-0.5.0'
$coverPS1="$coverPath\SQLCover.ps1"
$coverDLL="$coverPath\SQLCover.dll"
$server='GTR_DEV16'
$database='GTR'
$SQLuser='gtr_write'
$SQLpass='removed'
$query="EXEC tSQLt.Run 'zut_clinvar_sub_curation.Test_update_sub_date'"
$connectionString = "User ID=$SQLuser;Password=$SQLpass;database=$database;Server=$server;"
Unblock-File -Path $coverDll
Add-Type -Path $coverDll
. $coverPS1
$coverage = new-object SQLCover.CodeCoverage ($connectionString, $database, $null, $true)
$result = $coverage.Cover($query)
Export-OpenXml $result "testout.xml"

Output

.\short_test.ps1

Starting Code Coverage
Starting Code Coverage...Done
Executing Command: EXEC tSQLt.Run 'zut_clinvar_sub_curation.Test_update_sub_date'
Executing Command: EXEC tSQLt.Run 'zut_clinvar_sub_curation.Test_update_sub_date'...done
Stopping Code Coverage
Exception calling "Cover" with "1" argument(s): "Exception gathering the results"
At C:\Users\hoffman\Documents\Apps\SQLcover\short_test.ps1:19 char:1
+ $result = $coverage.Cover($query)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : SqlCoverException

Adding a second argument, 0, didn't help.

lastlink commented 4 years ago

@ncbidoug I would try the SQLCoverCore project using dotnet core 3.1. You can also use the debugger in visual studio code to track down your issue. I think correcting your powershell script would take more time than using the easier dll.

Example using dotnet core:

cd src/SQLCover/SqlCoverCore
mkdir -p TestCoverageOpenXml
dotnet run -- -c Get-CoverTSql -e Export-OpenXml -k "Server=localhost;Database=master;User ID=sa;Password=JdMsKZPBBA8kVFXVrj8d" -d DatabaseWithTests -q "tSQLt.runAll" -o TestCoverageOpenXml

you can also call the new dll directly, (not to be confused w/ the one targeting .net461

cd src/SQLCover/SqlCoverCore
dotnet build
.\bin\Debug\netcoreapp3.1\SQLCoverCore.exe -c Get-CoverTSql -e Export-OpenXml -k "Server=localhost;Database=master;User ID=sa;Password=JdMsKZPBBA8kVFXVrj8d" -d DatabaseWithTests -q "tSQLt.runAll" -o TestCoverageOpenXml
# or 
dotnet run .\bin\Debug\netcoreapp3.1\SQLCoverCore.dll -- -c Get-CoverTSql -e Export-OpenXml -k "Server=localhost;Database=master;User ID=sa;Password=JdMsKZPBBA8kVFXVrj8d" -d DatabaseWithTests -q "tSQLt.runAll" -o TestCoverageOpenXml