• Post Reply Bookmark Topic Watch Topic
  • New Topic

setting the current directory  RSS feed

 
paul wheaton
Trailboss
Posts: 22262
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've inherited a bit of code ... there are functional tests that expect to be run from a very particular directory. The innards of this thing are delicate and not to be touched. But I want to run these tests from ant, intellij, ant within intellij, and someday cruise control.

It seems like I just need to have the setUp() stuff set the current directory to be the directory that makes all that sort of thing happy.

I found an old thread where Jim Yingst says there is a way in the new "JDK 1.3" to set the current directory, but java ignores it.

What to do?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For what it's worth, in that old thread there's a way to change the effective directory seen by a new process spawned from a Runtime.exec() call. Which has no effect on what the rest of your Java program sees, as one would expect.

It might indeed be nice to be able to change the working directory programmatically in setUp(), but I don't know a way to do it. But other workarounds should be available.

Let's see... in Ant the junit task has a dir attribute that should do what you want. Fork needs to be enabled too. In IntelliJ you can go to the Run/Debug Configurations - find the green arrow near the middle of the top toolbar, move just left of that where there's a black down arrow; click it and select Edit Configurations. Change the "Working Directory" field to whatever you want. You may want to hit the Edit Defaults button at lower left, and change the working directory there.
[ March 09, 2006: Message edited by: Jim Yingst ]
 
paul wheaton
Trailboss
Posts: 22262
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IJ forgets the settings as move from test to test. Pain in the butt. Plus, I'm worried that people will find the tests clunky and choose to not run them.

I tried the System.setProperty("user.dir", weirdDir); and it didn't work (as you predicted).

Could there be something affiliated with a thread?
 
paul wheaton
Trailboss
Posts: 22262
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this bug at sun: http://bugs.sun.com/bugdatabase/view_bug.do;:WuuT?bug_id=4045688

Sounds like it will continue to be this way for a loooooong time.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be fair, that isn't technically a bug, but a new feature request.

In IntelliJ, did you try the Edit Defaults button? That should make it so any new test you run has the same working directory. In that project, anyway - doesn't seem to affect defaults for other projects. Which is a good thing.

If you get an ant task set up correctly, that should be very easy for anyone to run outside IntelliJ.

I know that's not quite what you want here, but that's all I can think of. Longer term, I'd look into refactoring the tests, and really all code that does any file IO, so that it goes through an intermediate file util class that allows you to specify an effective working directory. As well as facilitating mocking of all file operations, and adding numerous convenience methods to make IO easier. Something we've both done in the past of course, to some extent, though the bit about being able to specify a working directory is new.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!