0

I have a set of tests that involve loading files from the file-system and I would like to use the current filename to locate the test files relative to the test file.

The below code shows the pattern I am trying, but unfortunately when I run the test using M-x ert, it fails with (wrong-type-argument stringp nil), because both the load-file-name and buffer-file-name are nil. How should I approach this?

(ert-deftest closing-braces ()
  (find-file (concat (file-name-directory
                       (or load-file-name buffer-file-name))
                     "ClosingBrace.file"))
  ...
  )
4

A wild guess: put the file name in a defvar or a defconst, and use it in the function:

(defconst my-test-location (file-name-directory (or load-file-name buffer-file-name)))

(ert-deftest closing-braces ()
  (find-file (concat my-test-location "ClosingBrace.file")))

What you want is the value of load-file-name or buffer-file-name at compile/load time, but when you reference it within your function (ert-deftest is a fancy version of defun), you get the value at run time, which is not as useful. With defvar or defconst, you should be able to "lock in" the compile/load time value.

2

I've used the following construct:

(defvar objc-font-lock-test-setup-directory
  (if load-file-name
      (file-name-directory load-file-name)
   default-directory))

It works both when the file is loaded or when doing something like an eval-buffer.

  • Thanks, both ways seem to work, but I had to give the answer to legoscia for answering first – rneatherway Jun 23 '15 at 9:35
  • OK, fair enough! – Lindydancer Jun 23 '15 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.