Raku: Read file in test folder when using prove6
Asked Answered
J

1

9

When writing tests, text file gets read in the test folder but not outside the folder (i.e., when running prove6). For example, this code reads ReadConfig.ini inside the test folder, but not outside of it:

my %v = ReadIni( 'ReadConfig.ini' );

On the other hand, this code works outside the test folder:

my %v = ReadIni( $*PROGRAM.dirname.IO.add('ReadConfig.ini') );

What would the proper syntax?

Thanks!

Jazzy answered 24/4, 2019 at 11:54 Comment(2)
instead of .dirname.IO.add (up, then down), you can just say .sibling (sideways).Worthy
@CurtTilmes: Excellent. More readable, like: my %v1 = read_ini( $*PROGRAM.sibling('ReadIni_1.ini') );.Jazzy
A
9

So your test contains something like:

my %v = ReadIni( 'ReadConfig.ini' );

When you declare a relative file path it will be absolutified against $*CWD. So if you run your test inside the t/ folder it will look for t/ReadConfig.ini, and if you run it inside t/../ folder it will look for ../ReadConfig.ini. Therefore when you are writing tests you should use absolutified paths such that tooling is not constrained to running tests inside a specific location.


my %v = ReadIni( $*PROGRAM.dirname.IO.add('ReadConfig.ini') );

On the other hand if you provide an absolute file path then there is no discrepancy in what is meant, and this will Do What You Mean regardless of what directory you are in. However I would suggest no using dirname which does not include the volume portion on windows, and would instead use parent:

my %v = ReadIni( $*PROGRAM.parent.add('ReadConfig.ini') );
Atomy answered 24/4, 2019 at 14:3 Comment(1)
Thanks for the answer! I am on GNU/Linux, and I will stick with $*PROGRAM.sibling by now. Cheers!Jazzy

© 2022 - 2024 — McMap. All rights reserved.