pace_cli_test.vala 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* -*- Mode: Vala; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*- */
  2. using Pace;
  3. class PaceCliTest {
  4. public static int main ( string[] args ) {
  5. Test.init ( ref args );
  6. PaceCliTest.add_tests ();
  7. Test.run ();
  8. return 0;
  9. }
  10. public static void add_tests () {
  11. Test.add_func ( "/pace/cli", () => {
  12. string file_content = "# comment\n\n[options]\n#key = value\nkey2 = value2\noption\n\n" +
  13. "[repo1]\nServer = value\n\n" +
  14. "#[repo2]\n#Server = value\n#Include = value2";
  15. string file_name = "cli_test_pacman.conf";
  16. PaceCliTest.create_file ( file_name, file_content );
  17. Cli test_cli = new Cli ( file_name );
  18. assert ( test_cli is Cli );
  19. } );
  20. Test.add_func ( "/pace/cli/list", () => {
  21. FileStream stdout_temp = (owned)stdout;
  22. string file_content = "# comment\n\n[options]\n#key = value\nkey2 = value2\noption\n\n" +
  23. "[repo1]\nServer = value\n\n" +
  24. "#[repo2]\n#Server = value\n#Include = value2";
  25. string file_name = "cli_test_pacman_list.conf";
  26. PaceCliTest.create_file ( file_name, file_content );
  27. string[] args = {"", "list"};
  28. Cli test_cli_list = new Cli ( file_name );
  29. stdout = FileStream.open ( "pace-cli-list.stdout", "w" );
  30. test_cli_list.run ( args );
  31. stdout = (owned)stdout_temp;
  32. string pace_cli_list_stdout = PaceCliTest.read_file ( "pace-cli-list.stdout" );
  33. string expected_string = "repo1\nrepo2 (disabled)\n";
  34. assert ( pace_cli_list_stdout == expected_string );
  35. } );
  36. Test.add_func ( "/pace/cli/list_options", () => {
  37. FileStream stdout_temp = (owned)stdout;
  38. string file_content = "# comment\n\n[options]\n#key = value\nkey2 = value2\noption\n\n" +
  39. "[repo1]\nServer = value\n\n" +
  40. "#[repo2]\n#Server = value\n#Include = value2";
  41. string file_name = "cli_test_pacman_list_options.conf";
  42. PaceCliTest.create_file ( file_name, file_content );
  43. string[] args = {"", "list-options"};
  44. Cli test_cli_list_options = new Cli ( file_name );
  45. stdout = FileStream.open ( "pace-cli-list_options.stdout", "w" );
  46. test_cli_list_options.run ( args );
  47. stdout = (owned)stdout_temp;
  48. string pace_cli_list_options_stdout = PaceCliTest.read_file ( "pace-cli-list_options.stdout" );
  49. string expected_string = "key (no setted)\nkey2 value2\noption\n";
  50. assert ( pace_cli_list_options_stdout == expected_string );
  51. } );
  52. Test.add_func ( "/pace/cli/enable", () => {
  53. string file_content = "# comment\n\n[options]\n#key = value\nkey2 = value2\noption\n\n" +
  54. "[repo1]\nServer = value\n\n" +
  55. "#[repo2]\n#Server = value\n#Include = value2";
  56. string file_name = "cli_test_pacman_enable.conf";
  57. PaceCliTest.create_file ( file_name, file_content );
  58. string[] args = {"", "enable", "repo2"};
  59. Cli test_cli_list = new Cli ( file_name );
  60. test_cli_list.run ( args );
  61. string file_changed = read_file ( file_name );
  62. string expected_string = "# comment\n\n[options]\n# key = value\nkey2 = value2\noption\n\n" +
  63. "[repo1]\nServer = value\n\n" +
  64. "[repo2]\nServer = value\nInclude = value2\n";
  65. assert ( file_changed == expected_string );
  66. } );
  67. Test.add_func ( "/pace/cli/remove", () => {
  68. string file_content = "# comment\n\n[options]\n#key = value\nkey2 = value2\noption\n\n" +
  69. "[repo1]\nServer = value\n\n" +
  70. "#[repo2]\n#Server = value\n#Include = value2";
  71. string file_name = "cli_test_pacman_remove.conf";
  72. PaceCliTest.create_file ( file_name, file_content );
  73. string[] args = {"", "remove", "repo2"};
  74. Cli test_cli_list = new Cli ( file_name );
  75. test_cli_list.run ( args );
  76. string file_changed = read_file ( file_name );
  77. string expected_string = "# comment\n\n[options]\n# key = value\nkey2 = value2\noption\n\n" +
  78. "[repo1]\nServer = value\n";
  79. assert ( file_changed == expected_string );
  80. } );
  81. Test.add_func ( "/pace/cli/disable", () => {
  82. string file_content = "# comment\n\n[options]\n#key = value\nkey2 = value2\noption\n\n" +
  83. "[repo1]\nServer = value\n\n" +
  84. "#[repo2]\n#Server = value\n#Include = value2";
  85. string file_name = "cli_test_pacman_disable.conf";
  86. PaceCliTest.create_file ( file_name, file_content );
  87. string[] args = {"", "disable", "repo1"};
  88. Cli test_cli_list = new Cli ( file_name );
  89. test_cli_list.run ( args );
  90. string file_changed = read_file ( file_name );
  91. string expected_string = "# comment\n\n[options]\n# key = value\nkey2 = value2\noption\n\n" +
  92. "# [repo1]\n# Server = value\n\n" +
  93. "# [repo2]\n# Server = value\n# Include = value2\n";
  94. assert ( file_changed == expected_string );
  95. } );
  96. Test.add_func ( "/pace/cli/set/key/commented", () => {
  97. string file_content = "# comment\n\n[options]\n# key = value\nkey2 = value2\noption\n\n" +
  98. "[repo1]\nServer = value\n\n" +
  99. "# [repo2]\n# Server = value\n#Include = value2";
  100. string file_name = "cli_test_pacman_set.conf";
  101. PaceCliTest.create_file ( file_name, file_content );
  102. string[] args = {"", "set", "key", "test_value"};
  103. Cli test_cli_list = new Cli ( file_name );
  104. test_cli_list.run ( args );
  105. string file_changed = read_file ( file_name );
  106. string expected_string = "# comment\n\n[options]\nkey = test_value\nkey2 = value2\noption\n\n" +
  107. "[repo1]\nServer = value\n\n" +
  108. "# [repo2]\n# Server = value\n# Include = value2\n";
  109. assert ( file_changed == expected_string );
  110. } );
  111. Test.add_func ( "/pace/cli/set/key/uncommented", () => {
  112. string file_content = "# comment\n\n[options]\n# key = value\nkey2 = value2\noption\n\n" +
  113. "[repo1]\nServer = value\n\n" +
  114. "#[repo2]\n#Server = value\n#Include = value2";
  115. string file_name = "cli_test_pacman_set.conf";
  116. PaceCliTest.create_file ( file_name, file_content );
  117. string[] args = {"", "set", "key2", "test_value"};
  118. Cli test_cli_list = new Cli ( file_name );
  119. test_cli_list.run ( args );
  120. string file_changed = read_file ( file_name );
  121. string expected_string = "# comment\n\n[options]\n# key = value\nkey2 = test_value\noption\n\n" +
  122. "[repo1]\nServer = value\n\n" +
  123. "# [repo2]\n# Server = value\n# Include = value2\n";
  124. assert ( file_changed == expected_string );
  125. } );
  126. Test.add_func ( "/pace/cli/set/option", () => {
  127. string file_content = "# comment\n\n[options]\n#key = value\nkey2 = value2\n#option\n\n" +
  128. "[repo1]\nServer = value\n\n" +
  129. "#[repo2]\n#Server = value\n#Include = value2";
  130. string file_name = "cli_test_pacman_set.conf";
  131. PaceCliTest.create_file ( file_name, file_content );
  132. string[] args = {"", "set", "option"};
  133. Cli test_cli_list = new Cli ( file_name );
  134. test_cli_list.run ( args );
  135. string file_changed = read_file ( file_name );
  136. string expected_string = "# comment\n\n[options]\n# key = value\nkey2 = value2\noption\n\n" +
  137. "[repo1]\nServer = value\n\n" +
  138. "# [repo2]\n# Server = value\n# Include = value2\n";
  139. assert ( file_changed == expected_string );
  140. } );
  141. Test.add_func ( "/pace/cli/unset", () => {
  142. string file_content = "# comment\n\n[options]\n# key = value\nkey2 = value2\noption\n\n" +
  143. "[repo1]\nServer = value\n\n" +
  144. "# [repo2]\n# Server = value\n# Include = value2";
  145. string file_name = "cli_test_pacman_unset.conf";
  146. PaceCliTest.create_file ( file_name, file_content );
  147. string[] args = {"", "unset", "key2"};
  148. Cli test_cli_list = new Cli ( file_name );
  149. test_cli_list.run ( args );
  150. string file_changed = read_file ( file_name );
  151. string expected_string = "# comment\n\n[options]\n# key = value\n# key2 = value2\noption\n\n" +
  152. "[repo1]\nServer = value\n\n" +
  153. "# [repo2]\n# Server = value\n# Include = value2\n";
  154. assert ( file_changed == expected_string );
  155. } );
  156. Test.add_func ( "/pace/cli/unset/option", () => {
  157. string file_content = "# comment\n\n[options]\n#key = value\nkey2 = value2\noption\n\n" +
  158. "[repo1]\nServer = value\n\n" +
  159. "#[repo2]\n#Server = value\n#Include = value2";
  160. string file_name = "cli_test_pacman_unset_option.conf";
  161. PaceCliTest.create_file ( file_name, file_content );
  162. string[] args = {"", "unset", "option"};
  163. Cli test_cli_list = new Cli ( file_name );
  164. test_cli_list.run ( args );
  165. string file_changed = read_file ( file_name );
  166. string expected_string = "# comment\n\n[options]\n# key = value\nkey2 = value2\n# option\n\n" +
  167. "[repo1]\nServer = value\n\n" +
  168. "# [repo2]\n# Server = value\n# Include = value2\n";
  169. assert ( file_changed == expected_string );
  170. } );
  171. }
  172. private static void create_file ( string name, string content ) {
  173. try {
  174. FileUtils.set_contents ( name, content );
  175. } catch ( Error e ) {
  176. stderr.printf ( "Error creating config file (%s)\n", e.message );
  177. }
  178. }
  179. private static string read_file ( string file_name ) {
  180. string file_content = "";
  181. try {
  182. FileUtils.get_contents ( file_name, out file_content );
  183. } catch ( Error e ) {
  184. stderr.printf ( "Error opening config file (%s)\n", e.message );
  185. }
  186. return file_content;
  187. }
  188. }