How to get full path when testing a package


#1

I am writing tests for an Atom package and I have to open a text file (test.txt) which is at the same level of my package-spec.coffee file.

I used the following code to open my file:

atom.workspace.open('test.txt')
.then (content) ->
    text = content.buffer.getText()

However when I run the test with apm test the file is not opened because of relative paths. If I write the full path of the test.txt it works. How can I tell my script to use relative path, or how can I get the full path of my file. I cannot hardcode the full path because I want to run the tests with CircleCI, which has not the same paths as my local paths.

The __dirname command returns /usr/share/atom/resources/app.asar/static I do not undestand why…


#2

You can use resolvePackagePath() to get the path of your specific package, then tell Atom where to find the files from there.


#3

__dirname returns the absolute path to the directory containing the currently executing file. If you type __dirname at the JavaScript console, you will get /usr/share/atom/resources/app.asar/static or something similar (on my machine it is /Applications/Atom.app/Contents/Resources/app.asar/static) because that is the directory where the file that loads the general Atom environment lives (which is the context that the JavaScript console runs in). If you use __dirname from a file in your package, it will return the directory where that file lives no matter what machine the file is executed on.