Re: [go-nuts] read conf file under testing enviroment

1,027 views
Skip to first unread message

Antonio Dolcetta

unread,
Sep 23, 2012, 11:47:19 PM9/23/12
to golan...@googlegroups.com
Will it not be the same place where it is looking for the real file ?
testconfdir := filepath.Dir(os.Args[0])


On 09/24/2012 06:22 AM, Alessandro wrote:
> Hi all,
> I'm studying Go since few days ago, and I have a small problem.
>
> My testing program has to read a configuration file in the "current"
> directory. For "current" I mean the "executable" directory; I get it with
>
> executabledir := filepath.Dir(os.Args[0])
>
> then
> fconf := filepath.Join(executabledir, "TCDOCMNG.CONF")
>
> But all the tests fail for :
> Can't find file /tmp/go-build901160268/tcdocmng/_test/TCDOCMNG.CONF
>
> Eh, the temporary folder used for the test has not the configuration file!
>
> How can I solve? I think the best solution is to get that temporary
> folder and copy there the conf. file at the beginning of the test, but
> I don't know how to know which is the temporary folder.
> Other ideas?
>
> Thanks
>
>

Dave Cheney

unread,
Sep 24, 2012, 12:02:12 AM9/24/12
to Antonio Dolcetta, golan...@googlegroups.com
Args[0] is not guaranteed to be the fully qualified path to the
program, and frequently is not.

https://code.google.com/p/go/issues/detail?id=4057 might be of interest.
> --
>
>

Alessandro

unread,
Sep 24, 2012, 2:47:14 AM9/24/12
to golan...@googlegroups.com

On Monday, September 24, 2012 5:55:30 AM UTC+2, Antonio Dolcetta wrote:
Will it not be the same place where it is looking for the real file ?
testconfdir := filepath.Dir(os.Args[0])

Yes, exactly, I use it, but that is the problem.

Here the paths:
..prg_path../run_main.go  // <= here I read the conf file with filepath.Dir
..prg_path../run_main_test.go
..prg_path../TCDOCMNG.CONF


When you run the test the os.Args[0] is not "..prg_path../" , but is something like "/tmp/.../".

The os.Getwd() get the correct folder (because I run the test under  "..prg_path../" ) , but it will not work when I compile the run_main.go in an executable. In this last case I want to be free to run "run_main" from another folder: if I run "..prg_path../run_main" from the home, it will expect to find the conf file in the home.


Jan Mercl

unread,
Sep 24, 2012, 3:00:07 AM9/24/12
to Alessandro, golan...@googlegroups.com
On Mon, Sep 24, 2012 at 12:22 AM, Alessandro <dedal...@gmail.com> wrote:
> Other ideas?

$ FOO=$(pwd) ; go test # not tested

...
p := strings.Join(os.Getenv("FOO"), "TCDOCMNG.CONF")
...

-j

Rémy Oudompheng

unread,
Sep 24, 2012, 3:22:15 AM9/24/12
to Alessandro, golang-nuts
go test runs the binary in the sources dirctory. The usual pattern is
to have your data files in a testdata/ subdirectory, and access them
via a relative path. Search the standard library for examples.

Rémy.


2012/9/24, Alessandro <dedal...@gmail.com>:
> --
>
>
>

Rémy Oudompheng

unread,
Sep 24, 2012, 7:41:57 AM9/24/12
to Alessandro, golang-nuts
(please answer to the mailing-list)

The test binary is built in a temporary directory, but it is run from
the source directory, so it has easy access to the testdata/
subdirectory. Look at the source code of the compress/gzip package for
examples.

Rémy


2012/9/24, Alessandro <dedal...@gmail.com>:
>
> From: Rémy Oudompheng <remyoud...@gmail.com>
>
>> go test runs the binary in the sources dirctory. The usual pattern is
>
>
> http://golang.org/cmd/go/ : " The package is built in a temporary
> directory so it does not interfere with the non-test installation"
>
>
>> to have your data files in a testdata/ subdirectory, and access them
>> via a relative path. Search the standard library for examples.
>
> I'm searching, but I'm not lucky.
> I found something about the " -test.X flags", but..
>
> go test -x -v -cpuprofile=prof.out -dir=testdata -update
>
> will compile the test binary and then run it as
>
> pkg.test -test.v -test.cpuprofile=prof.out -dir=testdata -update
>
>
> So "-dir" is a flag that has to be managed by my run_main.go.
> And how can I manage it? Have I attach "testdir" to Getwd()? I don't
> want it.
>
>
> Can you please link me an example?
>
Reply all
Reply to author
Forward
0 new messages