Code Contracts trying to get build errors instead of warnings - code-contracts

I'm trying to get VS2010 Ultimate with Code Contracts to generate Errors instead of Warnings.
I have this simple test program:
using System.Diagnostics.Contracts;
namespace MyError
{
public class Program
{
static void Main(string[] args)
{
Program prog = new Program();
prog.Log(null);
}
public void Log(string msg)
{
Contract.Requires(msg != null);
}
}
}
It correctly determines there is a violation of the contract:
C:\...\Program.cs(10,13): warning : CodeContracts: requires is false: msg != null
In my csproj file there is this property field for Debug:
TreatWarningsAsErrors>true
Is there something else I have to set in the project settings to turn these into errors?

It looks like at this point Microsoft has elected not to make this possible, but they are considering it for the future:
http://connect.microsoft.com/VisualStudio/feedback/details/646880/code-contracts-dont-listen-to-treat-warnings-as-errors-setting

The problem is that the code contracts use a rewriter. they show as warnings because they are only calculated after the build completes.
Well i don't really know how it works, but unless you built code contracts into the compiler i do not see how they could be anything but warnings / messages.

Related

Why C#8 Default implementations of interface members will report an error

Why C#8 Default implementations of interface members will report an error?
public interface Logger
{
void Info(string message);
void Error(string message);
// C#8 Default implementations of interface
void Warn(string message)
{
// "interface method cannot declare a body" error message
}
}
and
.NET Core 3.0 is configured as shown in the screenshot.
This is a Resharper/Rider bug: https://youtrack.jetbrains.com/issue/RSRP-474628
The feature is sound and your setup is correct. Also the following works for me, i can compile and run the following in .Net Core 3
class Program
{
interface IDefaultInterfaceMethod
{
void DefaultMethod()
{
Console.WriteLine("I am a default method in the interface!");
}
}
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
Note though, i get your error in the IDE!! yet there was no error in the error window. However, it still compiles and runs so it is not C#8.. Seeing this is a sure-sign something else is the issue
In Short, this is likely a Resharper problem, when i suspended Resharper the false positive went away

WordPerfect COM Automation Error

In c# .Net 4.0 I am attempting to automate WordPerfect.
To do this I add a reference in my project to the wpwin14.tlb file that lives in the WordPerfect program folder.
That has the effect of creating the COM interfaces within my project.
Next I should be able to write code that instantiates a WordPerfect.PerfectScript object that I can use to automate WordPerfect.
However, when I try to instantiate the WordPerfect.PerfectScript object c# throws the error:
"Unable to cast COM object of type 'System.__ComObject' to interface
type 'WordPerfect.PerfectScript'. This operation failed because the
QueryInterface call on the COM component for the interface with IID
'{C0E20006-0004-1000-0001-C0E1C0E1C0E1}' failed due to the following
error: The RPC server is unavailable. (Exception from HRESULT:
0x800706BA)."
The thing to zero in on in that message (I do believe) is that the RPC server is unavailable.
I have tried this with WordPerfect running in the background and without. And I have gone to my services and made sure that RPC services were all running and restarting everything.
Is it possible that I am getting blocked by a firewall? That is my only faintest guess
I just wrap it as an OLE call and clean up my COM object with FinalReleaseComObject.
Here's a simple wrapper class I've been using to open Wp docs and convert them to pdf. It cleans up nicely in our automated process:
public class WpInterop : IDisposable
{
private bool _disposed;
private PerfectScript _perfectScript;
public PerfectScript PerfectScript
{
get
{
if (_perfectScript == null)
{
Type psType = Type.GetTypeFromProgID("WordPerfect.PerfectScript");
_perfectScript = Activator.CreateInstance(psType) as PerfectScript;
}
return _perfectScript;
}
}
protected void Dispose(bool disposing)
{
if (disposing)
{
Marshal.FinalReleaseComObject(_perfectScript);
}
_disposed = true;
}
public void Dispose()
{
if (_disposed == false)
{
GC.SuppressFinalize(this);
Dispose(true);
}
}
}
Make sure your version of WordPerfect has all of the service packs and hot fixes installed. This step has fixed many random-sounding issues for me over the years. Looks like you are using X4, which is no longer supported by Corel, which means that the updates are no longer on its web site. You should be running version 14.0.0.756 (SP2 plus 2 hotfixes).
I just uninstalled WPX4 and re-installed it, without the service pack updates. Running this code gave the exact error as the OP:
using System.Runtime.InteropServices;
using WordPerfect;
namespace WP14TLB
{
class Program
{
static void Main(string[] args)
{
PerfectScript ps = new PerfectScript();
ps.WPActivate();
ps.KeyType("Hello WP World!");
Marshal.ReleaseComObject(ps);
ps = null;
}
}
}
Installing the service packs "magically" fixed the problem.
BTW, for future reference, you can also try the WPUniverse forums. There are quite a few WP experts who regularly answer difficult questions.
There is also a link to the X4 updates here:

NUnit Inconclusive Confusion

I have the following:-
[TestFixture]
class TaskServiceTest
{
public void Implements_ITaskService()
{
var service = CreateService();
Assert.That(service, Is.InstanceOf<ITaskService>());
}
private static ITaskService CreateService()
{
return null;
}
}
When I run that in Visual Studio / Resharper It is reported as 'Inconclusive'. The explanation of which in the NUnit Docs is
The Assert.Inconclusive method indicates that the test could not be completed with the data available. It should be used in situations where another run with different data might run to completion, with either a success or failure outcome.
I don't see that holding here, so can anyone explain what I am doing wrong?
Thanks
I just realised that it is because I missed the [Test] attribute off of the unit test.
[Test]
public void Implements_ITaskService()
{
var service = CreateService();
Assert.That(service, Is.InstanceOf<ITaskService>());
}

BeforeFeature/AfterFeature does not work using SpecFlow and Coded UI

I am not able to define a [BeforeFeature]/[AfterFeature] hook for my feature file. The application under test is WPF standalone desktop applications.
If I use [BeforeScenario]/[AfterScenario] everything works fine, the application starts without any problem, the designed steps are performed correctly and the app is closed.
Once I use the same steps with [BeforeFeature]/[AfterFeature] tags the application starts and the test fails with:
The following error occurred when this process was started: Object reference not set to an instance of an object.
Here is an example:
[Binding]
public class Setup
{
[BeforeScenario("setup_scenario")]
public static void BeforeAppScenario()
{
UILoader.General.StartApplication();
}
[AfterScenario("setup_scenario")]
public static void AfterAppScenario()
{
UILoader.General.CloseApplication();
}
[BeforeFeature("setup_feature")]
public static void BeforeAppFeature()
{
UILoader.General.StartApplication();
}
[AfterFeature("setup_feature")]
public static void AfterAppFeature()
{
UILoader.General.CloseApplication();
}
}
StartApplication/CloseApplication were recorded and auto-generated with Coded UI Test Builder:
public void StartApplication()
{
// Launch '%ProgramFiles%\...
ApplicationUnderTest Application = ApplicationUnderTest.Launch(this.StartApplicationParams.ExePath, this.StartApplicationParams.AlternateExePath);
}
public class StartApplicationParams
{
public string ExePath = "C:\\Program Files..."
public string AlternateExePath = "%ProgramFiles%\\..."
}
Noteworthy: I'm quite new with SpecFlow.
I can't figure it out why my test fails with [BeforeFeature] and works fine with [BeforeScenario].
It would be great if somebody could help me with this issue. Thanks!
I ran into a similar problem recently. Not sure if this can still help you, but it may be of use for people who stumble upon this question.
For BeforeFeature\AfterFeature to work, the feature itself needs to be tagged, tagging just specific scenarios will not work.
Your feature files should start like this:
#setup_feature
Feature: Name Of Your Feature
#setup_scenario
Scenario: ...

SSJS to call a method in java class (in java library)

I've created a java library (named: invoke) with a java class (Invoke). Its seen when expanding Script libraries under code in the designer navigation pane.
The code is:
package com.kkm.vijay;
public class Invoke {
public static void main(String[] args) {
Runtime r = Runtime.getRuntime();
Process p = r.exec("C://some.exe");
}
}
Used the following ssjs to an onclick event of a button shows Error:500 when previewed in browser.
importPackage(com.kkmsoft.vijay);
var v=new Invoke();
v.main();
Even i used a function inside the class and changed the last line of ssjs to v.fn(). Yet the same problem.
There are a number of things wrong, and as Fredrik mentions you should switch on the standard Error page.
Your first code won't run because it is not correctly capturing the Exception. You are also using a main() method, which is normally used to execute a program. But you are calling it without any arguments. Avoid using that method unless it is for executing an application.
So change it to this:
package com.kkm.vijay;
import java.io.IOException;
public class Invoke {
public void mainCode() {
Runtime r = Runtime.getRuntime();
try {
Process p = r.exec("C://WINDOWS//notepad.exe");
} catch (IOException e) {
e.printStackTrace();
}
}
}
You should put that code in the new Java view in Designer.
Next your button code needs to change.
var v=new com.kkm.vijay.Invoke();
v.mainCode();
Testing that it should work fine. The issue next is, as it is SSJS the application will execute on the server. There may be security implications in this, and it may need you to modify the java.policy file in order to do this.
The related permission will be java.io.FilePermission.

Resources