]> Kevux Git Server - kevux-tools/commitdiff
Progress: Continue working on completing the remove program.
authorKevin Day <Kevin@kevux.org>
Tue, 25 Mar 2025 03:01:12 +0000 (22:01 -0500)
committerKevin Day <Kevin@kevux.org>
Tue, 25 Mar 2025 03:01:12 +0000 (22:01 -0500)
Finish some work that I thought I finished in the previous commit.
There were some commented out cases where I was doing controlled manual testing and I forgot to restore the commented out logic.

The Time and EpochTime are not well documented and are also not actually implemented!
Add more of the Time and EpochTime documentation.
I will look into implementing this at a later date.

Add unit tests for the basic Unix Date format.

sources/c/program/kevux/tools/remove/main/common/string.c
sources/c/program/kevux/tools/remove/main/common/string.h
sources/c/program/kevux/tools/remove/main/convert.c
sources/c/program/kevux/tools/remove/main/print/message.c
tests/unit/remove/c/test-remove-date_changed.c
tests/unit/remove/c/test-remove-date_changed.h
tests/unit/remove/c/test-remove.c

index 9023b721a5c2bddfc1670fc4ff0a5a64497f6c5a..c63348ab54408bcd4b93e6b2be19b038fe3c9828 100644 (file)
@@ -76,6 +76,11 @@ extern "C" {
   const f_string_static_t kt_remove_date_format_example_13_s = macro_f_string_static_t_initialize_1(KT_REMOVE_date_format_example_13_s, 0, KT_REMOVE_date_format_example_13_s_length);
   const f_string_static_t kt_remove_date_format_example_14_s = macro_f_string_static_t_initialize_1(KT_REMOVE_date_format_example_14_s, 0, KT_REMOVE_date_format_example_14_s_length);
 
+  const f_string_static_t kt_remove_date_time_example_00_s = macro_f_string_static_t_initialize_1(KT_REMOVE_date_time_example_00_s, 0, KT_REMOVE_date_time_example_00_s_length);
+  const f_string_static_t kt_remove_date_time_example_01_s = macro_f_string_static_t_initialize_1(KT_REMOVE_date_time_example_01_s, 0, KT_REMOVE_date_time_example_01_s_length);
+  const f_string_static_t kt_remove_date_time_example_02_s = macro_f_string_static_t_initialize_1(KT_REMOVE_date_time_example_02_s, 0, KT_REMOVE_date_time_example_02_s_length);
+  const f_string_static_t kt_remove_date_time_example_03_s = macro_f_string_static_t_initialize_1(KT_REMOVE_date_time_example_03_s, 0, KT_REMOVE_date_time_example_03_s_length);
+
   const f_string_static_t kt_remove_date_symbol_equal_s = macro_f_string_static_t_initialize_1(KT_REMOVE_date_symbol_equal_s, 0, KT_REMOVE_date_symbol_equal_s_length);
   const f_string_static_t kt_remove_date_symbol_less_s = macro_f_string_static_t_initialize_1(KT_REMOVE_date_symbol_less_s, 0, KT_REMOVE_date_symbol_less_s_length);
   const f_string_static_t kt_remove_date_symbol_less_equal_s = macro_f_string_static_t_initialize_1(KT_REMOVE_date_symbol_less_equal_s, 0, KT_REMOVE_date_symbol_less_equal_s_length);
index 78fc8e5933408aeb02efa4a19ba0255f334977e0..4d19542bfa15d54e86edb502944a548ac2ac382b 100644 (file)
@@ -177,6 +177,12 @@ extern "C" {
  * - 13: The string "YYYY/MM".
  * - 14: The string "YYYY/".
  *
+ * kt_remove_date_time_*_s:
+ * - 00: An EpochTime format with an explicit year.
+ * - 01: A Time format with an explicit year.
+ * - 02: An EpochTime format with an implicit year.
+ * - 03: A Time format with an implicit year.
+ *
  * kt_remove_date_symbol_*_s:
  * - equal:      Equal to operator.
  * - less:       Less than operator.
@@ -290,6 +296,21 @@ extern "C" {
   extern const f_string_static_t kt_remove_date_format_example_13_s;
   extern const f_string_static_t kt_remove_date_format_example_14_s;
 
+  #define KT_REMOVE_date_time_example_00_s "0::1672429502"
+  #define KT_REMOVE_date_time_example_01_s "1970:1672429502000000000"
+  #define KT_REMOVE_date_time_example_02_s "::1672429502"
+  #define KT_REMOVE_date_time_example_03_s ":1672429502000000000"
+
+  #define KT_REMOVE_date_time_example_00_s_length 16
+  #define KT_REMOVE_date_time_example_01_s_length 24
+  #define KT_REMOVE_date_time_example_02_s_length 12
+  #define KT_REMOVE_date_time_example_03_s_length 20
+
+  extern const f_string_static_t kt_remove_date_time_example_00_s;
+  extern const f_string_static_t kt_remove_date_time_example_01_s;
+  extern const f_string_static_t kt_remove_date_time_example_02_s;
+  extern const f_string_static_t kt_remove_date_time_example_03_s;
+
   #define KT_REMOVE_date_symbol_equal_s      "=="
   #define KT_REMOVE_date_symbol_less_s       "<"
   #define KT_REMOVE_date_symbol_less_equal_s "<="
index 8329628ac5023907d9f060c83c59bc1cb2c9aef9..1959a2b014df4b1b9bc69aaad92bff313ba0bd64 100644 (file)
@@ -52,6 +52,8 @@ extern "C" {
       return;
     }
 
+    // @todo add Time and EpochTime processing here.
+
     uint8_t matches = 0;
     f_range_t range_first = f_range_t_initialize;
     f_range_t range_second = f_range_t_initialize;
@@ -350,7 +352,17 @@ extern "C" {
       } // for
     }
 
-    main->setting.state.status = matches ? F_okay : F_status_set_error(F_known_not);
+    // Exact dates have the start and stop times set to the same values.
+    if (matches) {
+      date->stop_year = date->start_year;
+      date->stop_second = date->start_second;
+      date->stop_nanosecond = date->start_nanosecond;
+
+      main->setting.state.status = matches;
+    }
+    else {
+      main->setting.state.status = F_status_set_error(F_known_not);
+    }
   }
 #endif // _di_kt_remove_convert_date_
 
index 8be027431d6ab4098ed9963a25d6b4c3573242ee..f28ba86089ee592d156521f864d491a503526b37 100644 (file)
@@ -165,6 +165,12 @@ extern "C" {
     fl_print_format("  - '%[%r%]':                   A date like '%[%r%]'.%r", print->to, context.set.notable, kt_remove_date_format_13_s, context.set.notable, context.set.notable, kt_remove_date_format_example_13_s, context.set.notable, f_string_eol_s);
     fl_print_format("  - '%[%r%]':                     A date like '%[%r%]'.%r%r", print->to, context.set.notable, kt_remove_date_format_14_s, context.set.notable, context.set.notable, kt_remove_date_format_example_14_s, context.set.notable, f_string_eol_s, f_string_eol_s);
 
+    fl_print_format("  Valid formats for the '%[Time%]' and '%[EpochTime%]' formats might look like the following:%r", print->to, context.set.notable, context.set.notable, context.set.notable, context.set.notable, f_string_eol_s);
+    fl_print_format("  - '%[%r%]':          An EpochTime with an explicit year.%r", print->to, context.set.notable, kt_remove_date_time_example_00_s, context.set.notable, f_string_eol_s);
+    fl_print_format("  - '%[%r%]': A Time with an explicit year.%r", print->to, context.set.notable, kt_remove_date_time_example_01_s, context.set.notable, f_string_eol_s);
+    fl_print_format("  - '%[%r%]':             An EpochTime with an implicit year (relative to the UNIX Epoch).%r", print->to, context.set.notable, kt_remove_date_time_example_02_s, context.set.notable, f_string_eol_s);
+    fl_print_format("  - '%[%r%]':     A Time with an implicit year (relative to the current year).%r%r", print->to, context.set.notable, kt_remove_date_time_example_03_s, context.set.notable, f_string_eol_s, f_string_eol_s);
+
     fl_print_format("  The '%[%r%r%]' parameter accepts either ", print->to, context.set.notable, f_console_symbol_long_normal_s, kt_remove_long_remember_s, context.set.notable, f_string_eol_s);
     fl_print_format("%[%r%]", print->to, context.set.notable, kt_remove_yes_s, context.set.notable);
     fl_print_format(" or %[%r%] to designate whether or not to remember already pocessed paths.%r", print->to, context.set.notable, kt_remove_no_s, context.set.notable, f_string_eol_s);
index b817b7fb66f9d09f95f2b3470d7541a5246e5258..6f5ab60eee363c9b06d37ef839dd516e82015e48 100644 (file)
@@ -7,6 +7,158 @@
 extern "C" {
 #endif
 
+void test__kt_remove__date_changed__date_works(void **state) {
+
+  mock_unwrap = 0;
+  mock_unwrap_f_time_clock_get = 0;
+
+  const f_string_static_t target = macro_f_string_static_t_initialize_1("to_remove", 0, 9);
+
+  const uint8_t types_total = 8;
+  const uint8_t dates_total = 18;
+  const uint8_t params_total = 15;
+
+  struct stat stats[types_total];
+
+  memset(stats, 0, sizeof(struct stat) * types_total);
+
+  stats[0].st_mode = F_file_mode_all_d | F_file_type_block_d;
+  stats[1].st_mode = F_file_mode_all_d | F_file_type_character_d;
+  stats[2].st_mode = F_file_mode_all_d | F_file_type_directory_d;
+  stats[3].st_mode = F_file_mode_all_d | F_file_type_fifo_d;
+  stats[4].st_mode = F_file_mode_all_d | F_file_type_link_d;
+  stats[5].st_mode = F_file_mode_all_d | F_file_type_regular_d;
+  stats[6].st_mode = F_file_mode_all_d | F_file_type_socket_d;
+  stats[7].st_mode = F_file_mode_all_d & ~S_IFMT;
+
+  const f_string_static_t parameters[] = {
+    kt_remove_date_format_example_00_s,
+    kt_remove_date_format_example_01_s,
+    kt_remove_date_format_example_02_s,
+    kt_remove_date_format_example_03_s,
+    kt_remove_date_format_example_04_s,
+    kt_remove_date_format_example_05_s,
+    kt_remove_date_format_example_06_s,
+    kt_remove_date_format_example_07_s,
+    kt_remove_date_format_example_08_s,
+    kt_remove_date_format_example_09_s,
+    kt_remove_date_format_example_10_s,
+    kt_remove_date_format_example_11_s,
+    kt_remove_date_format_example_12_s,
+    kt_remove_date_format_example_13_s,
+    kt_remove_date_format_example_14_s,
+  };
+
+  const f_string_static_t operators[] = {
+    kt_remove_date_symbol_equal_s,
+    kt_remove_date_symbol_equal_s,
+    kt_remove_date_symbol_equal_s,
+    kt_remove_date_symbol_less_s,
+    kt_remove_date_symbol_less_s,
+    kt_remove_date_symbol_less_s,
+    kt_remove_date_symbol_less_equal_s,
+    kt_remove_date_symbol_less_equal_s,
+    kt_remove_date_symbol_less_equal_s,
+    kt_remove_date_symbol_more_s,
+    kt_remove_date_symbol_more_s,
+    kt_remove_date_symbol_more_s,
+    kt_remove_date_symbol_more_equal_s,
+    kt_remove_date_symbol_more_equal_s,
+    kt_remove_date_symbol_more_equal_s,
+    kt_remove_date_symbol_not_s,
+    kt_remove_date_symbol_not_s,
+    kt_remove_date_symbol_not_s,
+  };
+
+  const f_time_spec_t time_spec_dates[] = {
+    macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Equal
+    macro_f_time_spec_t_initialize_1(2 * kt_remove_time_seconds_in_year_d, 789),
+    macro_f_time_spec_t_initialize_1(1672429502, 0),
+    macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Less
+    macro_f_time_spec_t_initialize_1(2 * kt_remove_time_seconds_in_year_d, 789),
+    macro_f_time_spec_t_initialize_1(1672429502, 0),
+    macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Less Equal
+    macro_f_time_spec_t_initialize_1(2 * kt_remove_time_seconds_in_year_d, 789),
+    macro_f_time_spec_t_initialize_1(1672429502, 0),
+    macro_f_time_spec_t_initialize_1(2 * 86400, 345), // More
+    macro_f_time_spec_t_initialize_1(2 * kt_remove_time_seconds_in_year_d, 789),
+    macro_f_time_spec_t_initialize_1(1672429502, 0),
+    macro_f_time_spec_t_initialize_1(2 * 86400, 345), // More Equal
+    macro_f_time_spec_t_initialize_1(2 * kt_remove_time_seconds_in_year_d, 789),
+    macro_f_time_spec_t_initialize_1(1672429502, 0),
+    macro_f_time_spec_t_initialize_1(2 * 86400, 345), // Not
+    macro_f_time_spec_t_initialize_1(2 * kt_remove_time_seconds_in_year_d, 789),
+    macro_f_time_spec_t_initialize_1(1672429502, 0),
+  };
+
+  bool time_spec_removes[] = {
+    F_false, // Equal
+    F_false,
+    F_false,
+    F_true,  // Less
+    F_false,
+    F_false,
+    F_true,  // Less Equal
+    F_false,
+    F_false,
+    F_false, // More
+    F_true,
+    F_true,
+    F_false, // More Equal
+    F_true,
+    F_true,
+    F_true,  // Not
+    F_true,
+    F_true,
+  };
+
+  {
+    uint8_t type = 0;
+    uint8_t date = 0;
+    uint8_t param = 0;
+
+    for (; type < types_total; ++type) {
+
+      for (date = 12; date < dates_total; ++date) {
+
+        for (param = 0; param < params_total; ++param) {
+
+          const f_string_t argv[] = { "mocked_main", target.string, "-" KT_REMOVE_short_changed_s, operators[date].string, parameters[param].string, 0 };
+
+          stats[type].st_ctim = time_spec_dates[date];
+
+          // Pre-process file.
+          will_return(__wrap_f_file_exists, F_true);
+          will_return(__wrap_f_file_is, macro_f_file_type_is_link(stats[type].st_mode));
+          will_return(__wrap_f_file_stat, &stats[type]);
+          will_return(__wrap_f_file_stat, F_okay);
+
+          if (macro_f_file_type_is_directory(stats[type].st_mode)) {
+            will_return(__wrap_f_directory_empty, F_true);
+          }
+
+          if (time_spec_removes[date]) {
+            if (macro_f_file_type_is_directory(stats[type].st_mode)) {
+              will_return(__wrap_fl_directory_do, 1);
+              will_return(__wrap_fl_directory_do, &target);
+              will_return(__wrap_fl_directory_do, &target);
+              will_return(__wrap_fl_directory_do, f_directory_recurse_do_flag_action_e | f_directory_recurse_do_flag_directory_e | f_directory_recurse_do_flag_top_after_e);
+              will_return(__wrap_f_directory_remove, F_okay);
+            }
+            else {
+              will_return(__wrap_f_file_remove, F_okay);
+            }
+          }
+
+          const int result = kt_main_test__remove(5, argv, 0);
+
+          assert_int_equal(result, 0);
+        } // for
+      } // for
+    } // for
+  }
+}
+
 void test__kt_remove__date_changed__now_works(void **state) {
 
   mock_unwrap = 0;
@@ -89,7 +241,7 @@ void test__kt_remove__date_changed__now_works(void **state) {
   const f_time_spec_t time_spec_clocks = macro_f_time_spec_t_initialize_1(2 * 86400, 123);
 
   bool time_spec_removes[] = {
-    F_true, // Equal
+    F_true,  // Equal
     F_false,
     F_false,
     F_false,
@@ -97,7 +249,7 @@ void test__kt_remove__date_changed__now_works(void **state) {
     F_true,
     F_false,
     F_true,
-    F_true, // Less Equal
+    F_true,  // Less Equal
     F_true,
     F_false,
     F_true,
@@ -105,7 +257,7 @@ void test__kt_remove__date_changed__now_works(void **state) {
     F_false,
     F_true,
     F_false,
-    F_true, // More Equal
+    F_true,  // More Equal
     F_false,
     F_true,
     F_false,
@@ -243,7 +395,7 @@ void test__kt_remove__date_changed__today_works(void **state) {
   const f_time_spec_t time_spec_clocks = macro_f_time_spec_t_initialize_1(2 * 86400, 123);
 
   bool time_spec_removes[] = {
-    F_true, // Equal
+    F_true,  // Equal
     F_true,
     F_false,
     F_false,
@@ -251,7 +403,7 @@ void test__kt_remove__date_changed__today_works(void **state) {
     F_false,
     F_false,
     F_true,
-    F_true, // Less Equal
+    F_true,  // Less Equal
     F_true,
     F_false,
     F_true,
@@ -259,7 +411,7 @@ void test__kt_remove__date_changed__today_works(void **state) {
     F_false,
     F_true,
     F_false,
-    F_true, // More Equal
+    F_true,  // More Equal
     F_true,
     F_true,
     F_false,
@@ -401,23 +553,23 @@ void test__kt_remove__date_changed__tomorrow_works(void **state) {
     F_false,
     F_true,
     F_false,
-    F_true, // Less
+    F_true,  // Less
     F_true,
     F_false,
     F_true,
-    F_true, // Less Equal
+    F_true,  // Less Equal
     F_true,
     F_true,
     F_true,
     F_false, // More
     F_false,
-    F_true,
+    F_false,
     F_false,
     F_false, // More Equal
     F_false,
     F_true,
     F_false,
-    F_true, // Not
+    F_true,  // Not
     F_true,
     F_false,
     F_true,
@@ -427,9 +579,9 @@ void test__kt_remove__date_changed__tomorrow_works(void **state) {
     uint8_t type = 0;
     uint8_t date = 0;
 
-    for (; type < 1/*types_total*/; ++type) {
+    for (; type < types_total; ++type) {
 
-      for (date = 10; date < 11/*dates_total*/; ++date) {
+      for (date = 0; date < dates_total; ++date) {
 
         const f_string_t argv[] = { "mocked_main", target.string, "-" KT_REMOVE_short_changed_s, operators[date].string, parameter.string, 0 };
 
@@ -563,15 +715,15 @@ void test__kt_remove__date_changed__yesterday_works(void **state) {
     F_false,
     F_false,
     F_true,
-    F_true, // More
+    F_true,  // More
     F_true,
     F_true,
     F_false,
-    F_true, // More Equal
+    F_true,  // More Equal
     F_true,
     F_true,
     F_true,
-    F_true, // Not
+    F_true,  // Not
     F_true,
     F_true,
     F_false,
index 44f86d898feee9fb402e61c97f19cf28d953dd85..bf43afe51c14e2cdc04d01c51abcdba814dca6e1 100644 (file)
 #define _TEST__KT_remove__date_changed
 
 /**
- * Test that the remove works when the --changed parameter with "now" parameter are passed.
+ * Test that the remove works when the --changed parameter with date parameters passed.
+ */
+extern void test__kt_remove__date_changed__date_works(void **state);
+
+/**
+ * Test that the remove works when the --changed parameter with "now" parameter passed.
  */
 extern void test__kt_remove__date_changed__now_works(void **state);
 
 /**
- * Test that the remove works when the --changed parameter with "today" parameter are passed.
+ * Test that the remove works when the --changed parameter with "today" parameter passed.
  */
 extern void test__kt_remove__date_changed__today_works(void **state);
 
 /**
- * Test that the remove works when the --changed parameter with "tomorrow" parameter are passed.
+ * Test that the remove works when the --changed parameter with "tomorrow" parameter passed.
  */
 extern void test__kt_remove__date_changed__tomorrow_works(void **state);
 
 /**
- * Test that the remove works when the --changed parameter with "yesterday" parameter are passed.
+ * Test that the remove works when the --changed parameter with "yesterday" parameter passed.
  */
 extern void test__kt_remove__date_changed__yesterday_works(void **state);
 
index 3a1ecf508dc64d60ee109378a394fbe7868629b2..c5f3a5c7c95f4eabf4a878d274816aba23031817 100644 (file)
@@ -22,6 +22,7 @@ int main(void) {
     cmocka_unit_test(test__kt_remove__print_help__works),
     cmocka_unit_test(test__kt_remove__print_version__works),
 
+    cmocka_unit_test(test__kt_remove__date_changed__date_works),
     cmocka_unit_test(test__kt_remove__date_changed__now_works),
     cmocka_unit_test(test__kt_remove__date_changed__today_works),
     cmocka_unit_test(test__kt_remove__date_changed__tomorrow_works),