Trog-Config.t 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. use strict;
  2. use warnings;
  3. use Test::More;
  4. use Cwd();
  5. use File::Path();
  6. use File::Temp();
  7. use Path::Tiny();
  8. use FindBin;
  9. use lib "$FindBin::Bin/../lib";
  10. # $Trog::Config::default is read exactly once below, which is enough for perl
  11. # to suspect a typo.
  12. no warnings qw{once};
  13. require_ok('Trog::Config') or BAIL_OUT("Can't find SUT");
  14. # Every path in this app is cwd-relative, so exercise it the way the app sees
  15. # it: from inside an instance root. CLEANUP => 0 plus an explicit END, because
  16. # a tempdir cannot tidy itself up while we are standing inside it.
  17. our $ROOT = File::Temp::tempdir( 'tcms-config-XXXXXX', TMPDIR => 1, CLEANUP => 0 );
  18. our $OLDCWD = Cwd::getcwd();
  19. END {
  20. chdir($OLDCWD) if $OLDCWD;
  21. File::Path::remove_tree($ROOT) if $ROOT && -d $ROOT;
  22. }
  23. File::Path::make_path("$ROOT/config");
  24. chdir($ROOT) or die $!;
  25. subtest 'the defaults are where the app actually keeps them' => sub {
  26. # The whole bug this test exists for: get() used to hardcode the 'config/'
  27. # prefix internally, so $home_cfg was a bare basename and every writer that
  28. # used it put the file somewhere get() would never look.
  29. is( $Trog::Config::home_cfg, 'config/main.cfg', 'home_cfg is a usable path' );
  30. is( $Trog::Config::default, 'config/default.cfg', 'and so is the default' );
  31. like( $Trog::Config::home_cfg, qr{^config/}, 'home_cfg is under config/' );
  32. unlike( $Trog::Config::home_cfg, qr{^/}, 'and is relative, so it follows the instance root' );
  33. };
  34. subtest 'a config written to home_cfg is the one that gets read back' => sub {
  35. Path::Tiny->new('config/default.cfg')->spew_utf8("[general]\ndata_model=FlatFile\ntitle=Stock\n");
  36. # This is the round trip that was broken: write where the writers write,
  37. # read where the readers read, and check they agree.
  38. require Config::Simple;
  39. my $written = Config::Simple->new( syntax => 'ini' );
  40. $written->param( 'general.data_model', 'FlatFile' );
  41. $written->param( 'general.title', 'Instance' );
  42. $written->param( 'general.hostname', 'spec.example.com' );
  43. ok( $written->write($Trog::Config::home_cfg), 'wrote to home_cfg' );
  44. ok( -f 'config/main.cfg', 'which landed under config/, not the instance root' );
  45. ok( !-f 'main.cfg', 'and not loose in the root where nothing would read it' );
  46. my $read = Trog::Config::get();
  47. is( $read->param('general.title'), 'Instance', 'get() picked up the written config' );
  48. is( $read->param('general.hostname'), 'spec.example.com', 'including a key the defaults never had' );
  49. };
  50. subtest 'the shipped defaults are left alone' => sub {
  51. # get() is memoized, so this is the same object as above -- which is the
  52. # point: bin/tcms-hostname used to call save() on it, and save() writes back
  53. # to whichever file it was constructed from.
  54. is( Path::Tiny->new('config/default.cfg')->slurp_utf8,
  55. "[general]\ndata_model=FlatFile\ntitle=Stock\n",
  56. 'default.cfg is untouched' );
  57. };
  58. done_testing();