6

I am learning some programming interview questions so I've coded FizzBuzz. I'm trying to comprare the output of my program to a known good output that's in a .txt file.

How do I check that node fizzbuzz.js output is line by line equal to a expected-output.txt file, or even diff them?

I've tried this as suggested in the duplicate question:

diff -u expected-output.txt <(node fizzbuzz.js)

but diff outputs nothing and never seems to quit until I ^C. The program by itself runs fine, and so does diff -u expected-output.txt <(cat test.txt), it just doesn't seem to play well with node for some reason.

Chef Tony
  • 455
  • 1
    Possible duplicate of https://unix.stackexchange.com/q/11733/342404 – LL3 Jul 14 '19 at 16:47
  • 2
    Note: Unix does not have text files, they are just files. Text file were from CP/M, then MS-Dos adopted the idea, even though it did not really have a difference between text and binary files. – ctrl-alt-delor Jul 14 '19 at 16:52
  • 1
  • 1
    Hmm, I can't see why node.js would break with process substitution. A quick test works on my machine. Does running node fizzbuzz.js > output.txt; diff -u expected-output.txt output.txt work? If you can't get the process subst working, you might want to post another question about that (and include your version of node and a full .js script) – ilkkachu Jul 14 '19 at 17:57
  • @ilkkachu yeah, your command works fine. I'm using console.log() to print from my loop so maybe the process substitution never ends? – Chef Tony Jul 14 '19 at 18:05
  • I've posted here also: https://unix.stackexchange.com/questions/530129/process-substitution-with-doesnt-work-with-diff-and-node – Chef Tony Jul 14 '19 at 18:11
  • @ChefTony, well, if the program ever finishes, the process substitution pipes would close and diff would see end-of-file there. Getting it left hanging would pretty much need starting some background process with a copy of the pipe file descriptors... – ilkkachu Jul 14 '19 at 19:17

1 Answers1

11

In Bash/ksh/Zsh:

diff -u file.txt <(some command)

The <(some command) construct is called process substitution, and it makes the output from some command available as if from a file, so diff can read it. (It sets up pipes from the command and expands to the name of a named pipe or /dev/fd/N).

Similarly, >(some command) could be used to redirect writes to the command.

ilkkachu
  • 138,973