]> git.neil.brown.name Git - edlib.git/commitdiff
Introduce edlib_testing().
authorNeilBrown <neil@brown.name>
Mon, 19 Jun 2023 11:34:18 +0000 (21:34 +1000)
committerNeilBrown <neil@brown.name>
Wed, 28 Jun 2023 07:51:41 +0000 (17:51 +1000)
Instead of checking getenv("EDLIB_TESTING") in various places, combine
it into a single interface.

Signed-off-by: NeilBrown <neil@brown.name>
13 files changed:
DOC/TODO.md
core-editor.c
core.h
doc-dir.c
emacs-search.c
lang-python.c
lib-config.c
lib-linecount.c
lib-messageline.c
python/lib-autospell.py
python/lib-glibevents.py
python/lib-make.py
python/lib-shellcmd.py

index 18f747291f0b163d055f079ae86be1387a6a2909..9350457493205dff3e17047e8d7598f6ff1bb19b 100644 (file)
@@ -13,6 +13,8 @@ the file.
       instead of statics.  Then maybe times_up() can use pane_too_long()
 - [X] mark DEF_CMD structs as const- NO, ->refcnt is not constant.
 - [X] rexel: don't abort if something looks wrong, just fail.
+- [X] check EDLIB_TESTING just once and set a flag in editor, accessible
+      as pane_testing(): p->root->data(cast)->testing.
 
 ### Small
 
index 262063acb1d68007a6c62d40f730b4d6ab7f66c5..ad0d0ad40f16f3600a3ed443804715a0820e6019 100644 (file)
@@ -23,6 +23,7 @@ struct ed_info {
        struct mark *mark_free_list;
        struct map *map safe;
        struct lookup_cmd cmd;
+       bool testing;
        struct store {
                struct store *next;
                int size;
@@ -30,6 +31,12 @@ struct ed_info {
        } *store;
 };
 
+bool edlib_testing(struct pane *p safe)
+{
+       struct ed_info *ei = pane_root(p)->data;
+       return ei->testing;
+}
+
 DEF_LOOKUP_CMD(ed_handle, ed_map);
 
 DEF_CMD(global_set_attr)
@@ -496,6 +503,7 @@ struct pane *editor_new(void)
 
        alloc(ei, pane);
        ei->magic = ED_MAGIC;
+       ei->testing = (getenv("EDLIB_TESTING") != NULL);
        if (! (void*) ed_map) {
                ed_map = key_alloc();
                key_add(ed_map, "global-set-attr", &global_set_attr);
diff --git a/core.h b/core.h
index 5ce003934dc3749ae444403c7dea7deeef436964..7ca999b1db3739ebcee63b91f25d2acdb3d675e4 100644 (file)
--- a/core.h
+++ b/core.h
@@ -83,7 +83,7 @@ struct pane {
         */
        unsigned int            timestamp;
 
-       struct pane             *root;
+       struct pane             *root safe;
        struct command          *handle;
        void                    *data safe;
        struct attrset          *attrs;
@@ -168,6 +168,7 @@ char *strsave(struct pane *p safe, const char *buf);
 char *strnsave(struct pane *p safe, const char *buf, int len);
 char * safe __strconcat(struct pane *p, const char *s1 safe, ...);
 #define strconcat(p, ...) __strconcat(p, __VA_ARGS__, NULL)
+bool edlib_testing(struct pane *p safe);
 
 /* This is declared here so sparse knows it is global */
 void edlib_init(struct pane *ed safe);
index f09fbfdb01a9bf972d7cc5bc5c306a7d8a2f44a4..efddc4694442e61488f850221e6b69ed5cb762e4 100644 (file)
--- a/doc-dir.c
+++ b/doc-dir.c
@@ -506,13 +506,12 @@ static char *save_str(struct dir_ent *de safe, char *str safe)
        return de->nbuf;
 }
 
-static char *fmt_date(struct dir_ent *de safe, time_t t)
+static char *fmt_date(struct dir_ent *de safe, time_t t, struct pane *p safe)
 {
        struct tm tm;
        time_t now = time(NULL);
-       char *testing = getenv("EDLIB_TESTING");
 
-       if (testing && *testing) {
+       if (edlib_testing(p)) {
                t = 1581382278;
                now = t;
        }
@@ -611,7 +610,7 @@ static const char *__dir_get_attr(struct pane *home safe, struct mark *m safe,
        } else if (strcmp(attr, "hsize") == 0) {
                get_stat(dr, de);
                if (strchr(".:d", de->ch) &&
-                   getenv("EDLIB_TESTING"))
+                   edlib_testing(home))
                        /* Size might not be reliable for testing */
                        return "DIR";
                return fmt_size(de, de->st.st_size);
@@ -620,19 +619,19 @@ static const char *__dir_get_attr(struct pane *home safe, struct mark *m safe,
                return fmt_num(de, de->st.st_mtime);
        } else if (strcmp(attr, "mdate") == 0) {
                get_stat(dr, de);
-               return fmt_date(de, de->st.st_mtime);
+               return fmt_date(de, de->st.st_mtime, home);
        } else if (strcmp(attr, "atime") == 0) {
                get_stat(dr, de);
                return fmt_num(de, de->st.st_atime);
        } else if (strcmp(attr, "adate") == 0) {
                get_stat(dr, de);
-               return fmt_date(de, de->st.st_atime);
+               return fmt_date(de, de->st.st_atime, home);
        } else if (strcmp(attr, "ctime") == 0) {
                get_stat(dr, de);
                return fmt_num(de, de->st.st_ctime);
        } else if (strcmp(attr, "cdate") == 0) {
                get_stat(dr, de);
-               return fmt_date(de, de->st.st_ctime);
+               return fmt_date(de, de->st.st_ctime, home);
        } else if (strcmp(attr, "uid") == 0) {
                get_stat(dr, de);
                return fmt_num(de, de->st.st_uid);
@@ -642,7 +641,7 @@ static const char *__dir_get_attr(struct pane *home safe, struct mark *m safe,
        } else if (strcmp(attr, "user") == 0) {
                char *n;
                get_stat(dr, de);
-               if (getenv("EDLIB_TESTING"))
+               if (edlib_testing(home))
                        return "User";
                n = pwname(de->st.st_uid);
                if (n)
@@ -652,7 +651,7 @@ static const char *__dir_get_attr(struct pane *home safe, struct mark *m safe,
        } else if (strcmp(attr, "group") == 0) {
                char *n;
                get_stat(dr, de);
-               if (getenv("EDLIB_TESTING"))
+               if (edlib_testing(home))
                        return "Group";
                n = grname(de->st.st_gid);
                if (n)
@@ -678,7 +677,7 @@ static const char *__dir_get_attr(struct pane *home safe, struct mark *m safe,
                case S_IFLNK: *c ++ = 'l'; break;
                default:      *c ++ = '?'; break;
                }
-               if (getenv("EDLIB_TESTING") && de->ch == ':')
+               if (edlib_testing(home) && de->ch == ':')
                        /* ".." might not be under control of the test */
                        mode = 0777;
                for (i = 0; i < 3; i++) {
index ddfa12002daae134e5f7ac48cc232e0ffac5409e..ba978469d651cc30efe0dca7ce649565ae06bce3 100644 (file)
@@ -1004,7 +1004,7 @@ static void queue_highlight_refresh(struct pane *p safe)
 {
        call_comm("event:free", p, &emacs_search_reposition_delayed);
        call_comm("event:timer", p, &emacs_search_reposition_delayed,
-                 getenv("EDLIB_TESTING") ? 50 : 500);
+                 edlib_testing(p) ? 50 : 500);
 }
 
 DEF_CMD(emacs_search_reposition)
index 2c2d9d99cfa44bb8497862958471623da0703f0f..98f5725f1db7094aacaa06812c15b2117eff4943 100644 (file)
@@ -2978,6 +2978,8 @@ void edlib_init(struct pane *ed safe)
 
        PyModule_AddIntConstant(m, "WEOF", 0x3FFFFF);
 
+       PyModule_AddIntConstant(m, "testing", edlib_testing(ed));
+
        Edlib_CommandFailed = PyErr_NewException("edlib.commandfailed", NULL, NULL);
        Py_INCREF(Edlib_CommandFailed);
        PyModule_AddObject(m, "commandfailed", Edlib_CommandFailed);
index e50938739b81318d2eecd48fc324172058670464..931b0a9caefe746f859585f5a325b840b51e5453 100644 (file)
@@ -192,12 +192,12 @@ static void add_trigger(struct config_data *cd safe, char *path safe,
        struct trigger *t = cd->last_trigger;
 
        if (strstarts(name, "TESTING ")) {
-               if (getenv("EDLIB_TESTING") == NULL)
+               if (!edlib_testing(cd->root))
                        return;
                name += 8;
        }
        if (strstarts(name, "NOTESTING ")) {
-               if (getenv("EDLIB_TESTING") != NULL)
+               if (edlib_testing(cd->root))
                        return;
                name += 10;
        }
index d01e258490f8a271db6ea20baee3d01a64b80fa7..50b09c68fcf8ee9d9cd249c4ba49629ed9f2662a 100644 (file)
@@ -39,7 +39,6 @@ static struct map *linecount_map;
 DEF_LOOKUP_CMD(handle_count_lines, linecount_map);
 
 static const int batch_marks = 10;
-static bool testing = False;
 
 struct count_info {
        int view_num;
@@ -195,7 +194,7 @@ static void count_calculate(struct pane *p safe,
        struct mark *m, *m2;
        char *disable;
 
-       if (testing)
+       if (edlib_testing(p))
                sync = True;
 
        disable = pane_attr_get(p, "linecount-disable");
@@ -284,7 +283,7 @@ static void count_calculate(struct pane *p safe,
                attr_set_int(&p->attrs, "lines", lines);
                attr_set_int(&p->attrs, "words", words);
                attr_set_int(&p->attrs, "chars", chars);
-               if (!testing)
+               if (!edlib_testing(p))
                        pane_notify("doc:status-changed", p);
        }
 }
@@ -422,9 +421,6 @@ void edlib_init(struct pane *ed safe)
        call_comm("global-set-command", ed, &count_lines, 0, NULL, "CountLines");
        call_comm("global-set-command", ed, &count_lines, 0, NULL, "CountLinesAsync");
 
-       if (getenv("EDLIB_TESTING"))
-               testing = True;
-
        if (linecount_map)
                return;
 
index 1061ace72990553af282f4c76c53b0c083355283..b460d9abe72926c676b849553136717b3fa2cc59 100644 (file)
@@ -171,7 +171,7 @@ DEF_CMD(messageline_notify)
        struct mlinfo *mli = ci->home->data;
        int wait_time = 7;
 
-       if (getenv("EDLIB_TESTING"))
+       if (edlib_testing(ci->home))
                wait_time = 0;
 
        if (mli->modal) {
@@ -216,9 +216,9 @@ DEF_CMD(messageline_line_refresh)
                char buf[80];
                time_t t;
                struct tm *tm;
-               char *testing = getenv("EDLIB_TESTING");
+
                t = time(NULL);
-               if (testing && *testing)
+               if (edlib_testing(ci->home))
                        t = 1581382278;
                tm = localtime(&t);
                if (tm)
@@ -255,7 +255,7 @@ static struct pane *do_messageline_attach(struct pane *p safe)
        }
        mli->line = mlp;
        pane_focus(ret);
-       if (getenv("EDLIB_TESTING") == NULL)
+       if (!edlib_testing(p))
                /* This can introduce unwanted variablitiy in tests */
                call_comm("event:timer", mli->line, &force_refresh, 15000);
 
index 09c71f14f1fa770977ea55b2ad8faab47d039348..4d7dce16c6739f3df67726f1e72acfbd810fa327 100644 (file)
@@ -278,7 +278,7 @@ class autospell_view(edlib.Pane):
         self.set_time()
         focus = focus.leaf
 
-        if 'EDLIB_TESTING' in os.environ:
+        if edlib.testing:
             remain = 20
         else:
             remain = 200
index c85c0c0aa4460e3c482ed77c664c57f034c22ac2..3c31107e4c16dd29a11ba61a461134dbc0d7594a 100644 (file)
@@ -24,7 +24,7 @@ class events(edlib.Pane):
         self.ev_num = 0
         self.dont_block = False
         self.maxloops = 10
-        if 'EDLIB_TESTING' in os.environ:
+        if edlib.testing:
             self.maxloops = -1
 
     def handle_close(self, key, focus, **a):
index e8e2e19a1bb6806ab0ac288fbe91ddbdf23c11e3..7ff1de3d45d0d1568fb62895c27d3b0e5c88bd53 100644 (file)
@@ -61,8 +61,7 @@ class MakePane(edlib.Pane):
 
         done = 0
         self.set_time()
-        testing = 'EDLIB_TESTING' in os.environ
-        while testing or (done < 100 and not self.too_long()):
+        while edlib.testing or (done < 100 and not self.too_long()):
             # Look for one of:
             # filename:linenum:.....
             # filename:linenum ....
index 4e415b2747c5664b027e8b93a1ef1f6a9c23a209..5f87d48129e4f1e84e02b2f9130856cb8303039a 100644 (file)
@@ -79,7 +79,7 @@ class ShellPane(edlib.Pane):
         self.call("doc:set:doc-status", "Running")
         self.call("doc:notify:doc:status-changed")
         fd = self.pipe.stdout.fileno()
-        if 'EDLIB_TESTING' not in os.environ:
+        if not edlib.testing:
             fl = fcntl.fcntl(fd, fcntl.F_GETFL)
             fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
             self.call("event:read", fd, self.read)
@@ -94,7 +94,7 @@ class ShellPane(edlib.Pane):
         if not self.pipe:
             return edlib.Efalse
         try:
-            if 'EDLIB_TESTING' not in os.environ:
+            if not edlib.testing:
                 r = os.read(self.pipe.stdout.fileno(), 4096)
             else:
                 r = b''