# Config.datadir during testing

**URL:** <https://www.ruby-forum.com/t/config-datadir-during-testing/176537>\
**Category:** Ruby\
**Created:** [October 1, 2009, 8:16pm UTC](https://www.ruby-forum.com/t/config-datadir-during-testing/176537 "2009-10-01T20:16:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 1, 2009, 8:16pm UTC](https://www.ruby-forum.com/t/config-datadir-during-testing/176537/1 "2009-10-01T20:16:33Z")

</div>

How do I deal with Config.datadir during testing? I don’t want it to  
point to where the Gem will be, once it has been installed, but to the  
”data” directory in my current work directory.

I figured that I’d override Config.datadir

module Config  
def self.datadir(package)  
parts = File.expand\_path(’.’).split(/[#{Regexp.escape  
(File::SEPARATOR + File::ALT\_SEPARATOR)}]/)  
until parts.empty?  
break if parts.last == package  
end  
return (package) if parts.empty?  
parts.push ‘data’, package  
File.join(\*parts)  
end  
end

but the question is what I should replace with. I first  
figured I’d do

module ConfigWrapper  
def self.datadir(package)  
parts = File.expand\_path(’.’).split(/[#{Regexp.escape  
(File::SEPARATOR + File::ALT\_SEPARATOR)}]/)  
until parts.empty?  
break if parts.last == package  
end  
return super(package) if parts.empty?  
parts.push ‘data’, package  
File.join(\*parts)  
end  
end

Config.extend(ConfigWrapper),

but that won’t work, as that won’t put ConfigWrapper in the right  
place in the call chain (obvious, in retrospect). So I figured I’d do

module Config  
class \<\< self  
alias old\_datadir datadir  
end  
def self.datadir(package)  
parts = File.expand\_path(’.’).split(/[#{Regexp.escape  
(File::SEPARATOR + File::ALT\_SEPARATOR)}]/)  
until parts.empty?  
break if parts.last == package  
end  
return old\_datadir(package) if parts.empty?  
parts.push ‘data’, package  
File.join(\*parts)  
end  
end

but that doesn’t work, as old\_datadir will simply point to the current  
Config.datadir implementation, thus resulting in an infinite loop.

I see no way around it.

What should I do?

(I’d be very pleased if I didn’t have to modify Config.datadir, so if  
there’s a solution without it that would be nice. I would also like  
to see a solution to the problem of overriding a module method.)

Thanks!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 2, 2009, 11:15am UTC](https://www.ruby-forum.com/t/config-datadir-during-testing/176537/2 "2009-10-02T11:15:45Z")

</div>

On Oct 1, 8:12 pm, I wrote:

> How do I deal with Config.datadir during testing? I don’t want it to  
> point to where the Gem will be, once it has been installed, but to the  
> ”data” directory in my current work directory.

OK, since no one has responded, can I perhaps limit the question to  
how to deal with things like Config.datadir during testing for the  
package that I’m testing? Before installation I want it to be local  
to the package. Once installed I want it to use the path where the  
files were installed.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 2, 2009, 3:02pm UTC](https://www.ruby-forum.com/t/config-datadir-during-testing/176537/3 "2009-10-02T15:02:18Z")

</div>

On Oct 1, 8:12 pm, “removed\_email\_address@domain.invalid”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> until parts.empty?
> 
> ```
> 
> end  
> end

OK, I’m a fucking moron. That should be

while last = parts.pop  
break if last == package  
end

Still, it would be nice to see alternative solutions to this.
