I want to create a gem that extends the functionality of Rake, creating commands for compiling .NET code.*
Basically, I want to be able to write a Rakefile like this:
desc "Build main executable"
fsc :build do |t|
t.output_file = "nde.exe"
t.output_folder = "output"
t.source_files << "main.fs"
t.packages << "packages/Nancy"
end
task :default => [:clean, :build]
So just like the file
function in rake creates a FileTask, the fsc
function should create a compilation task.
I created a POC where I have the code inlined in the Rakefile – and it gives me an idea on how to do it. The next step is to isolate it into a gem.
The central point in my POC is the following function:
def create_compilation_task
task_dependencies = source_files | assembly_refs
dest = File.join output_folder, output_file
Rake::FileTask::define_task dest => task_dependencies do |t|
refs = assembly_refs.map { |r| "-r:#{r}" }.join(" ")
output = "--out:#{dest}"
target = "--target:exe"
sources = source_files.join(" ")
system "fsharpc #{output} #{refs} #{target} #{sources}"
end
end
But how do I write tests for such a gem?
I created this skeleton test code:
describe 'F# compiler' do
it 'compiles code' do
@app = Rake::Application.new
Rake.application = @app
Rake::Task::define_task :build do
end
@app[:build].invoke()
end
end
This does indeed call the task defined in the context of the test. Are there any problems writing the test code in this manner?
Independence from file system
The problem domain is that of files on the disk, and internally I have used FileTask
objects to represent both input and output files, as I can take advantage of Rake’s build optimization feature.
But how do I test this, e.g. verifying that the output is indeed not generated when it is newer than all inputs. I could verify the chain of Task
instances being created, but that would be testing an implementation detail. I would much rather execute the task and verify the behavior.
Is there a way to start the Rake infrastructure up with a mocked file system, or do I create a temporary folder on disk during a test run?
Verifying system commands being called
I use the system
function to call an external program. I would very much like to avoid calling the F# compiler during testing, so instead I need to capture the call system
call.
Verifying the call should also not be too specific, i.e. it shouldn’t determine if the command is equal to some expected string. When I verify that the compiler has been called with the correct source files, that test must not fail if compiler optimization options have been modified.
I imagine that using a standard CLI argument parser gem could be used.
* I am aware of the Albacore gem, but that compiles msbuild projects, where I want to compile source code files directly.