Relative Paths and testing

time to read 6 min | 1193 words

While testing NQA, I need to connect to a database to make sure that everything works. I do most of my work unconnected, and I use several machines, so I wanted to use a file base test database (access, sqlite, firebird-embedded, etc).

That, in itself, was not a problem. But NQA doesn't do anything special for file based databases, which is at it should be, since if the user would want to use a file based database, s/he can just put the full path. But that is not an option for me, for the simple reason that I don't know the full path of the database until I compile (different machine, different paths, etc).

I thought that I was stuck doing some really nasty things until I figure out that I was stupid, and I could just write a ten line script that would change the path at compliation time. This is actually quite handy for a lot of reasons, since there are a lot of tools that require full paths where I would rather use a relative one. Now all I need to do is put a "{0}" where I want the full path to be, and run this tool.

One thing that bit me was the fact that the shell uses \" to escape the " sign, so when I wrote things like: Replace "C:\Code\File.Config" "C:\Code\Bin\Debug\" "C:\Code Built\Bin\Debug\File.Config", what the application got was:

  • C:\Code\File.Config
  • C:\Code\Bin\Debug" "C:\Code
  • Built\Bin\Debug\File.Config"

Took me quite by surpirse, I must say, even though it's logical. You need to escape this by using "\\".

Anyway, here is the code. There is no error handling, but there is no need for one:

public static void Main (string[] args)

{

      string content;

      foreach (string arg in args)

            Console.WriteLine(arg);

      if(args.Length<3)

      {

            Console.WriteLine("AssureXmlPath: <source file> <string to replace in file> <destination file>");

            return;

      }

      using(StreamReader file = File.OpenText(args[0]))

            content = file.ReadToEnd();

      content = string.Format(content,args[1]);

      using(StreamWriter output = new StreamWriter(File.OpenWrite(args[2])))

            output.Write(content);

      Console.WriteLine("Wrote File: " +args[2]);

}