Trog-Config.t 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. use 5.040;
  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(
  55. Path::Tiny->new('config/default.cfg')->slurp_utf8,
  56. "[general]\ndata_model=FlatFile\ntitle=Stock\n",
  57. 'default.cfg is untouched'
  58. );
  59. };
  60. subtest 'the defaults describe what is editable' => sub {
  61. # The /config editor is built out of this, so the parse has to keep the
  62. # comments (they are the help text) and the file order (they are the form).
  63. Path::Tiny->new('config/schema.cfg')->spew_utf8(<<'CFG');
  64. [general]
  65. # FlatFile or SQLite.
  66. # Pick one.
  67. data_model=FlatFile
  68. title=Stock
  69. [security]
  70. # Where embeds may come from.
  71. allow_embeds_from=vimeo.com *.vimeo.com
  72. CFG
  73. my $schema = Trog::Config::schema('config/schema.cfg');
  74. is_deeply( [ map { $_->{section} } @$schema ], [qw{general security}], 'the sections come back in file order' );
  75. my $general = $schema->[0]{fields};
  76. is_deeply( [ map { $_->{key} } @$general ], [qw{data_model title}], 'and so do the keys in them' );
  77. is( $general->[0]{name}, 'general.data_model', 'a field knows its full name' );
  78. is( $general->[0]{default}, 'FlatFile', 'and what the defaults ship' );
  79. is( $general->[0]{comment}, 'FlatFile or SQLite. Pick one.', 'a comment block above a key is that key\'s help' );
  80. is( $general->[1]{comment}, '', 'and does not leak onto the key after it' );
  81. is( $schema->[1]{fields}[0]{default}, 'vimeo.com *.vimeo.com', 'values with spaces survive intact' );
  82. };
  83. done_testing();