Unable to use the same relative path in my program AND my unit tests

Jean Lebrument

In my Go Project I use a function that opens a specific file and returns its content. The file is stored in another directory but still inside my project directory.

package infrastructure

func openKey() ([]byte, error) {
    if path, err := filepath.Abs("../security/key.rsa"); err != nil {
        return nil, err
    } 

    return ioutil.ReadFile(path)
}

This function works if I call it from a unit test. But if I call the same function in my program, I've this error:

2015/08/13 15:47:54 open /me/go/src/github.com/myaccount/security/key.rsa: no such file or directory

The correct absolute path of the file is:

/me/go/src/github.com/myaccount/myrepo/security/key.rsa

Both code that use the openKey function (from my program and unit test) are in the same package: infrastructure

Here is how I execute my program:

go install && ../../../../bin/myproject

And how I execute my unit tests:

go test ./...

And finally the directory structure of my project:

go/src/github.com/myaccount/myrepo/: 
- main.go
- security:
    - key.rsa // The file that I want to open
    - ...
- infrastructure
    - openFile.go // The file with the func `openKey``
    - server.go // The file with the func that call the func `openKey`
    - openFile_test.go // The unit test that calls the func `openKey`

Edit:

Here are the absolute paths of where the binary of my program is located:

/Users/me/Documents/Développement/Jean/go/bin

And where my unit tests are located:

/var/folders/tj/8ywtc7pj3rs_j0y6zzldwh5h0000gn/T/go-build221890578/github.com/myaccount/myrepo/infrastructure/_test

Any suggestion?
Thanks!

Elwinar

First, you shouldn't use the same files when running your tests than when running your application in production. Because the test files are accessible to everyone that has access to the repository, which is a security fail.

As said in the comments, the problem is that when running your tests, the working directory is these of the source code (in fact, go copy the whole bunch into a temp directory prior to running the tests), while when you run the program for real, the working directory is the one you are running the command from, hence the wrong relative path.

What I would advise is to use a configuration option to get a the file from which load your file (or a base directory to use with your paths). Either using an environment variable (I strongly encourage you to do that, see the 12factors manofesto for details), a configuration file, a command-line flag, etc.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can't build with scons my unit tests alongside with the program

From Dev

Running program for all my tests file

From Dev

Unable to set images to open with my program, Why not?

From Dev

unable to find bug in my R program

From Dev

My C program unable to detect multiplication symbol

From Dev

Why HashSet order always same for my program?

From Dev

Why does my program return the same character?

From Dev

Using Gnu Parallel to run a folder of tests against my program

From Dev

How can I tell that my program is under unit test environment

From Dev

Is there a program that I can use to find my drivers?

From Dev

How to install JCommander and use it in my program?

From Dev

How to correctly use pointers in my c program

From Dev

My Java program - find path - does not output anything

From Dev

Installing a program through MAC Command Line, by putting binary in my PATH

From Dev

Bash is not finding a program even though it's on my path

From Dev

Why does Bash give "No such file or directory" for a program that's in my PATH?

From Dev

Unable to run my first node.js program?

From Dev

Unable to install My program. Mostly has to do with Crystal Reports

From Dev

Why is my powershell script unable to create a folder in C:\Program Files?

From Dev

Why do I get the same readout on my ASCII to number program?

From Dev

How could I get all of the sums of my program on the same line?

From Dev

Java Program keeps inserting the same line into my file

From Dev

Redirect program output to my program

From Dev

Address already in use for successive run of my server program

From Dev

Why should I use "setSeed()" in my java card program?

From Dev

How can I Identify which package classes are in use in my program?

From Dev

If I use MySQL would my program have to be open-source?

From Dev

What can be slowing down my program when i use multithreading?

From Dev

Is there any ways to use msinfo32.exe in my program?

Related Related

  1. 1

    Can't build with scons my unit tests alongside with the program

  2. 2

    Running program for all my tests file

  3. 3

    Unable to set images to open with my program, Why not?

  4. 4

    unable to find bug in my R program

  5. 5

    My C program unable to detect multiplication symbol

  6. 6

    Why HashSet order always same for my program?

  7. 7

    Why does my program return the same character?

  8. 8

    Using Gnu Parallel to run a folder of tests against my program

  9. 9

    How can I tell that my program is under unit test environment

  10. 10

    Is there a program that I can use to find my drivers?

  11. 11

    How to install JCommander and use it in my program?

  12. 12

    How to correctly use pointers in my c program

  13. 13

    My Java program - find path - does not output anything

  14. 14

    Installing a program through MAC Command Line, by putting binary in my PATH

  15. 15

    Bash is not finding a program even though it's on my path

  16. 16

    Why does Bash give "No such file or directory" for a program that's in my PATH?

  17. 17

    Unable to run my first node.js program?

  18. 18

    Unable to install My program. Mostly has to do with Crystal Reports

  19. 19

    Why is my powershell script unable to create a folder in C:\Program Files?

  20. 20

    Why do I get the same readout on my ASCII to number program?

  21. 21

    How could I get all of the sums of my program on the same line?

  22. 22

    Java Program keeps inserting the same line into my file

  23. 23

    Redirect program output to my program

  24. 24

    Address already in use for successive run of my server program

  25. 25

    Why should I use "setSeed()" in my java card program?

  26. 26

    How can I Identify which package classes are in use in my program?

  27. 27

    If I use MySQL would my program have to be open-source?

  28. 28

    What can be slowing down my program when i use multithreading?

  29. 29

    Is there any ways to use msinfo32.exe in my program?

HotTag

Archive