Tuesday, July 10, 2018

Creating Fake Data For Unit Tests With RimuTec.Faker

Setting up data for tests can be tedious. For example sometimes you may end up using first names like “First 1”, “First 2”, etc. Data like this may result in duplicates, so you then go to the next step using first names like “First “+ Guid.NewGuid() which may be a problem, too. Using such a name in a user interface may no longer fit the available space, where real-life names like “John” or “Anna” would fit easily.

Another aspect can be that you are required to have a most recent version of your system or product for demo purposes, let’s say a pre-sales situation or an internal product review. Trying having a conversation about a screen that says “First123456 Last645861”. Wouldn’t it be nice if it would display names such as “John Doe” or “Anna Karenina”? It most likely would also create a much better impression with prospects, customers or other stakeholders. Your software looks more “complete”.

Ruby developers have a gem named Faker which is available on github. Faker is based on a Perl library and has a large library of fake data generators.

There have been a few efforts to port the Ruby gem to .NET. Most of these have a limited set of generators and haven’t been maintained for years.

Therefore we decided to have another look. Where other projects use regular resource files, we decided to use the yaml files from Ruby’s Faker gem verbatim. By taking this approach we would automatically have some guardrails that would allow leveraging as many of the great ideas from the Faker gem as possible.

The result is RimuTec.Faker which offers the following classes of generators:
  • Address
  • Company
  • Educator
  • Finance
  • IDNumber
  • Internet
  • Job
  • Lorem
  • Name
  • PhoneNumber
  • RandomNumber
Obviously this is not the complete list yet. We release frequently and are happy to receive suggestions for priorities in terms of what should be added next. Just log an issue on github.

Using RimuTec.Faker the example from the beginning be written as follows:

You can get both, the source code as well as the NuGet package under the MIT license. This means you can use it for free and also use it in your own projects as you see fit, including commercial and closed-source projects.

Happy coding!

Friday, February 24, 2017

TypeScript Errors TS2300 and TS2374 in VS2017 RC

We recently updated to Visual Studio 2017 RC from Visual Studio 2013. I won't go into the details why we skipped Visual Studio 2015.

The upgrade at some point then trigger to update to the latest TypeScript toolset version (2.1 at the time of writing).

We then notice a series of TypeScript compiler errors with codes TS2300 and TS2374. It turns out that the compiler was actually happy. The error list is created by IntelliSense. To identify them as generated by IntelliSense you need to ensure that you select "IntelliSense Only" from the drop-down list in the "Error List" view of Visual Studio (VS) (click image to enlarge it):


In our case we observed the following errors:
  • TS2300 Duplicate identifier 'export='
  • TS2374 Duplicate string index signature
  • TS2375 Duplicate number index signature
When we check for the files for which these were recorded, we discovered that these were related to jQuery.d.ts, knockout.d.ts and index.d.ts. All of these files were part of the latest NuGet package for either jQuery or Knockout, so outside of our control.

After some experimentation we identified adding a tsconfig.json file to the project solved the problem.

Here is the complete content of the tsconfig.json file:

{
}

In other words: apart from the curly braces it is empty. We placed this file in the same directory as the project file (Web.csproj in our case) and included it in the project.

The file tsconfig.json does not have to be empty. You are free to add settings as needed. The key point for us was to avoid the false positive by IntelliSense.

With VS 2013 it worked without tsconfig.json. However, it looks as if the tooling has changed and that for IntelliSense to work correctly the file has to be present.

In closing, a gentle reminder to select "Build + IntelliSense" from the drop-down list in the "Error List" to see all errors again (if any). Here is an image that shows this as well as the location for the tsconfig.json file (click image to enlarge it):


Happy coding!

Sunday, August 09, 2015

Generating Resource Designer Files During Build with Visual Studio

In Visual Studio files containing resources have the extension *.resx. As you edit this file VS will use a custom tool to generate a strongly typed *designer.cs file. However, since this is a generated file, you should not need to put it under version control. Consequentially you will need to generate the file. One option is to use the context menu and select "Run Custom Tool". This is cumbersome if you check out the code base and have many resource files. It would be great, if those designer files were created automatically when building the project.

Another scenario is where resource files are re-used across multiple projects. Here, too, automatic generation during build would be useful.

Finally it would be great if a solution for automatically generating the designer files would work for both building the project in Visual Studio and building it via msbuild at the command line.

The solution presented here was developed with Visual Studio 2013 and C#. It may or may not work in other environments.

To automatically generate the *.designer.cs files from *.resx files, follow these steps:

1. Close your solution

2. Open as an XML file the project file in which you want to automatically generate the designer files. Note that you need to load it as an XML file. You can't edit these settings through the project property page.

3. Add a target to the project as follows:

<Target Name="GenerateDesignerFiles">
   <Message Text="Deleting old Designer Files..."/>
   <Delete Files="@(EmbeddedResource->'%(RootDir)%(Directory)%(Filename).resources')"/>
   <Delete Files="@(EmbeddedResource->'%(RootDir)%(Directory)%(Filename).designer.cs')"/>
   <Message Text="Generating Designer Files..."/>
   <GenerateResource
      Sources="@(EmbeddedResource)"
      StronglyTypedLanguage="C#"
      StronglyTypedClassName="%(Filename)"
      StronglyTypedNamespace="@(EmbeddedResource->'%(CustomToolNamespace)')"
      StronglyTypedFileName="@(EmbeddedResource->'%(RootDir)%(Directory)%(Filename).designer.cs')"
      PublicClass="true"
      >
   </GenerateResource>
   <Message Text="Generating Designer Files complete."/>
</Target>

4. Locate the target named "BeforeBuild". This target may be commented out (the default).

5. Modify the "BeforeBuild" target as follows:

<Target Name="BeforeBuild">
   <CallTarget Targets="GenerateDesignerFiles"/>
</Target>

This solution is based on all resource files being listed as "EmbeddedResource" within an ItemGroup of the project file, e.g.

<ItemGroup>
  <EmbeddedResource Include="Resources\Creditor\Display_Creditor.resx">
    <Generator>PublicResXFileCodeGenerator</Generator>
    <LastGenOutput>Display_Creditor.Designer.cs</LastGenOutput>
    <CustomToolNamespace>Acme.Web.Resources.Creditor</CustomToolNamespace>
  </EmbeddedResource>
  <EmbeddedResource Include="Resources\InboundEmail\Tooltip_InboundEmailDetails.resx">
    <Generator>PublicResXFileCodeGenerator</Generator>
    <LastGenOutput>Tooltip_InboundEmailDetails.Designer.cs</LastGenOutput>
    <CustomToolNamespace>Acme.Web.Resources.InboundEmail</CustomToolNamespace>
  </EmbeddedResource>
  <EmbeddedResource Include="Resources\Creditor\Tooltip_CreditorDetails.resx">
    <Generator>PublicResXFileCodeGenerator</Generator>
    <LastGenOutput>Tooltip_CreditorDetails.Designer.cs</LastGenOutput>
    <CustomToolNamespace>Acme.Web.Resources.Creditor</CustomToolNamespace>
  </EmbeddedResource>
</ItemGroup>

Disclaimer: This has been tested with Visual Studio 2013 and C# projects. It also works using msbuild. It may or may not work for other environments.

Tuesday, January 21, 2014

ASP.NET MVC, TypeScript, Azure Website and Git Deploy

The Issue

The issue and the workaround in this post applies to:

  • Visual Studio 2013
  • ASP.NET MVC using at least one TypeScript file
  • Azure Website with git deploy
  • Sites build and deployed before kudu issue 721 was resolved

Building a web site with ASP.NET and TypeScript targeting Azure Website I ran into an issue after I added the first TypeScript file and tried to deploy the web site using git deploy. It would fail with the following error message:

C:\DWASFiles\Sites\typescripttest\VirtualDirectory0\site\repository\TypeScriptHTMLApp1.csproj(67,3): error MSB4019: The imported project "D:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\TypeScript\Microsoft.TypeScript.targets" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk.

The root cause for this is that the file Microsoft.TypeScript.targets file is not yet available using git deploy for Azure Websites.

The issue that I ran into is documented and discussed in a few places, e.g.

Here is my workaround that solved the issue for now. Rumors has it that Microsoft is working on a proper solution for the issue.

Note that the workaround does not require a custom “deploy.cmd” file which I felt was a plus despite having creating custom deploy scripts for other projects and other reasons.

The Workaround

The workaround is based on making the msbuild task and the TypeScript compiler available when the msbuild script (aka solution file and project files) are executed.

In my repository I created a folder 3rdparty in parallel to the solution file.

Next I created a subfolder typescript-sdk and copied the content of “C:\Program Files (x86)\Microsoft SDKs\TypeScript” into “3rdparty\typescript-sdk”. The folder contains a file tsc.exe. Make sure this is version 0.9.1.1. This version worked for me. Apparently there are other versions out there. For example version 0.9.5.0 is reported to create an error.

I also needed the msbuild task. For that I created a subfolder typescript-task and copied the content of “C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\TypeScript” into “3rdparty\typescript-task”.

The next two steps required editing the file “Microsoft.TypeScript.targets” located in “3rdparty/typescript-task” and the ASP.NET MVC project file. I won’t detail these changes here as all of this workaround is available in a repository.

With this workaround I have the same behavior for Azure Website’s git deploy as I have locally building from Visual Studio 2013. To use a TypeScript file, all I have to do is adding the file, reference the resulting javascript in the appropriate place in my views or layouts, commit and push. No further steps required.

Once the issue is resolved, all I have to do is undo the changes in the project file and remove the folders “3rdparty\typescript-sdk” and “3rdparty\typescript-task” and everything else is back to normal.

This workaround should also be applicable for scenarios where you already have a custom deploy script. The workaround does not require any changes to custom deploy scripts.

All of the source code is available in a sample project at https://bitbucket.org/ml_agileutilities/typescript-sample. There is a branch with a version that allows reproducing the problem. There is a second branch that demonstrates that the workaround fixes the issue. If you want to try out either scenario create an Azure Website and have it use git deploy from one the respective branch.

Credits

I’d like to thank Thiago Almeida and David Ebbo, Microsoft, for their awesome support in finding and testing this workaround.

Although I didn’t need a custom deploy script in the end, I would also like to thank Amit Apple for his very helpful description of how to create custom deploy script for Azure Websites.

Monday, July 15, 2013

No Split Window for TypeScript?

In case you installed TypeScript for Visual Studio 2012 and are wondering why you are not getting the split window for a TypeScript file: Check whether you have installed “Web Essentials 2012” as well. The latter gives you the split windows.

We had a couple of cases in one of the teams I’m working with where installing Web Essentials resolved the issue.

Friday, May 24, 2013

Xamarin Studio: Hello, world! fails to build

I just purchased, installed and tried out Xamarin Studio. My motivation is that I want to try out this C#-based approach towards developing applications for Android.

Not that I want to move away from Visual Studio. I’m just working through a few examples and wanted to rule out VS as an influencing factor.

Problem is that the Hello World example already failed. The error message was “The “Aapt” task failed unexpectedly.” And there was also a “System.InvalidOperationException: Sequence contains no elements” and an error code “MSB4018”. Here is a screenshot:

image

To solve it launch the Android SDK Manager. In my case it showed that there was a new update for the Android DSF Platform-tool and also showed that the Android SDK Build-tools were not installed. I just left all checked/unchecked as it was and installed what was checked. Admittedly not very selective but it did the trick. Now it’s building.

Thursday, May 23, 2013

Visual Studio Image Library

Just tried to locate the Visual Studio Image Library. I couldn’t find it. Various sources suggest to locate the file VS2010ImageLibrary.zip. This doesn’t exist on my machine, though.

It turned out that I never had Visual Studio 2010 on this machine, only Visual Studio 2012 has been installed. It appears as if VS2012 doesn’t install it by default.

However, you can download the VS2012 Image Library here. Hopefully this post helps you saving some time.