Mock external executable


I’m writing a program that executes another program with different args and then makes decisions based on the stdout of it. I’m using my mac to program, however this other program runs on linux and anyway it wouldn’t be nice to write tests that use such program since would complicate CI, etc.

I thought I had the solution by making a rust file under the bin folder. That produces an executable with that name, and all I need to do there is to println! a mock of the other program. Genius! Or is it? I’m not sure if it’s a smart or dumb way of doing it!

A clear problem with that is that cargo runs on the root folder, and the mock program goes to /target/debug or wherever, depending if I’m debugging or not, and it get a random string appended to its filename, so i can’t seem to be able to target it with an hardcoded path.

I’ve looked into cargo, cargo.toml, etc, but I can’t find a way to just compile that bin executable to a set folder!



Thoughts? :thinking:


You just want the program that will get executed inside the other program to be at the root of the project is that it ?

If I understand you, you simply want to copy that program to the root. If it’s a random string that’s appended, I can see a simple copy with a regular expression of some sort working. Maybe a little execution script would do the job ?

(I’m sure someone will have a better idea)


The best option that I can think of is for it to go to the target folder itself, without the -r4nd0mstr1ng suffix. That way I know where it’s, and can hardcode the path in the test, while it doesn’t pollute the rest of the code.

I’m sure I could use a simple cp with a wildcard and possibly that will do it but it seems an overly complicated solution(especially cross-os wise). I’m hoping for some cargo magic here.