├── .github └── workflows │ └── test.yml ├── .gitignore ├── Changes ├── Makefile ├── README.md ├── init-loader.el └── test-init-loader.el /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | pull_request: 5 | push: 6 | 7 | jobs: 8 | build: 9 | runs-on: ubuntu-latest 10 | strategy: 11 | matrix: 12 | emacs_version: 13 | - 27.2 14 | - 28.2 15 | - 29.3 16 | - snapshot 17 | steps: 18 | - uses: purcell/setup-emacs@master 19 | with: 20 | version: ${{ matrix.emacs_version }} 21 | - uses: actions/checkout@v3 22 | - name: Run tests 23 | run: | 24 | make test 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.elc 2 | -------------------------------------------------------------------------------- /Changes: -------------------------------------------------------------------------------- 1 | Revision history for init-loader.el 2 | 3 | Revision 0.02 2014/10/31 syohex 4 | - Support MacPorts Emacs 5 | 6 | Revision 0.01 2014/08/29 syohex 7 | - Initial version for melpa-stable 8 | - Fix init-loader-show-log issue(Thanks k2nr) 9 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY : test 2 | 3 | EMACS ?= emacs 4 | 5 | test: 6 | $(EMACS) -Q -batch -L . -l test-init-loader.el -f ert-run-tests-batch-and-exit 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # init-loader.el [![Github Actions Status][github-actions-badge]][github-actions-link] [![melpa badge][melpa-badge]][melpa-link] [![melpa stable badge][melpa-stable-badge]][melpa-stable-link] 2 | 3 | 4 | ## What is it ? 5 | 6 | `init-loader.el` is a loader of configuration files. `init-loader.el` 7 | loads configuration files from specified directory. It enables you to 8 | categorize your configurations and separate them into multiple files. 9 | 10 | The original code is 11 | [here](http://coderepos.org/share/browser/lang/elisp/init-loader/init-loader.el). 12 | The current version of `init-loader.el` is based on 13 | [tarao's fork version](https://gist.github.com/tarao/4362564), which 14 | have some issues fixed and some essential features added compared with 15 | the original one. 16 | 17 | 18 | ## Sample code 19 | 20 | You only have to call `init-loader-load` with a directory where your 21 | configuration files are located. 22 | 23 | ```lisp 24 | (require 'init-loader) 25 | 26 | ;; Load configuration files in '/path/to/init-directory'. 27 | (init-loader-load "/path/to/init-directory") 28 | 29 | ;; If you omit arguments, then `init-loader-directory' is used 30 | (init-loader-load) 31 | ``` 32 | 33 | ## Features 34 | 35 | ### Files to be loaded 36 | 37 | Note that not all files in the directory are loaded. Each file is 38 | examined that if it is a .el or .elc file and, it has a valid name 39 | specified by `init-loader-default-regexp` or it is a platform specific 40 | configuration file. 41 | 42 | By default, valid names of configuration files start with two 43 | digits. For example, the following file names are all valid: 44 | - 00_util.el 45 | - 01_ik-cmd.el 46 | - 21_javascript.el 47 | - 99_global-keys.el 48 | 49 | Files are loaded in the lexicographical order. This helps you to 50 | resolve dependency of the configurations. 51 | 52 | A platform specific configuration file has a prefix corresponds to 53 | the platform. The following is the list of prefixes and platform 54 | specific configuration files are loaded in the listed order after 55 | non-platform specific configuration files. 56 | 57 | | Platform | Subplatform | Prefix | Example | 58 | | --------- | ----------------- | --------------- | --------------------------- | 59 | | Windows | | `windows-` | windows-fonts.el | 60 | | | Meadow | `meadow-` | meadow-commands.el | 61 | | Mac OS X | Carbon Emacs | `carbon-emacs-` | carbon-emacs-applescript.el | 62 | | | Cocoa Emacs | `cocoa-emacs-` | cocoa-emacs-plist.el | 63 | | GNU/Linux | | `linux-` | linux-commands.el | 64 | | *BSD | | `bsd-` | bsd-commands.el | 65 | | All | Non-window system | `nw-` | nw-key.el | 66 | 67 | ### Byte-compilation 68 | 69 | If `init-loader-byte-compile` is non-nil, each configuration file is 70 | byte-compiled when it is loaded. If you modify the .el file, then it 71 | is recompiled next time it is loaded. 72 | 73 | ### Log 74 | 75 | Loaded files and errors during the loading process are recorded. If 76 | `init-loader-show-log-after-init` is non-nil, the record is shown 77 | after the overall loading process. You can do this manually by `M-x 78 | init-loader-show-log`. 79 | 80 | ## Reference 81 | 82 | ### Interfaces 83 | 84 | #### `init-loader-load (&optional INIT-DIR)` 85 | 86 | Function to load configuration files in `INIT-DIR`. 87 | 88 | #### `init-loader-show-log ()` 89 | 90 | Interactive command to show log messages. 91 | 92 | ### Customization 93 | 94 | #### `init-loader-directory` : `directory` (default: `"~/.emacs.d/inits"`) 95 | 96 | Default directory of configuration files. 97 | 98 | #### `init-loader-show-log-after-init` : `boolean` (default: `t`) 99 | 100 | Show log message after initializing if this value is `t`. 101 | If this value is `error-only`, log buffer is shown only 102 | errors occured. 103 | 104 | #### `init-loader-byte-compile` : `boolean` (default: `nil`) 105 | 106 | Byte-compile configuration files if this value is non-nil. 107 | 108 | ### Hooks 109 | 110 | #### `init-loader-before-compile-hook` 111 | 112 | Abnormal hook run before byte-compiling a configuration file when 113 | `init-loader-byte-compile` is non-nil. Each function in the hook 114 | takes one argument, which is the name of the configuration file to be 115 | loaded. 116 | 117 | [github-actions-link]: https://github.com/emacs-jp/init-loader/actions 118 | [github-actions-badge]: https://github.com/emacs-jp/init-loader/workflows/CI/badge.svg 119 | [melpa-link]: https://melpa.org/#/init-loader 120 | [melpa-stable-link]: https://stable.melpa.org/#/init-loader 121 | [melpa-badge]: https://melpa.org/packages/init-loader-badge.svg 122 | [melpa-stable-badge]: https://stable.melpa.org/packages/init-loader-badge.svg 123 | -------------------------------------------------------------------------------- /init-loader.el: -------------------------------------------------------------------------------- 1 | ;;; init-loader.el --- Loader for configuration files 2 | 3 | ;; Author: IMAKADO 4 | ;; URL: https://github.com/emacs-jp/init-loader/ 5 | ;; Version: 0.02 6 | ;; Package-Requires: ((cl-lib "0.5")) 7 | 8 | ;; This file is free software; you can redistribute it and/or modify 9 | ;; it under the terms of the GNU General Public License as published by 10 | ;; the Free Software Foundation; either version 2, or (at your option) 11 | ;; any later version. 12 | 13 | ;; This file is distributed in the hope that it will be useful, 14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | ;; GNU General Public License for more details. 17 | 18 | ;; You should have received a copy of the GNU General Public License 19 | ;; along with GNU Emacs; see the file COPYING. If not, write to the 20 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 21 | ;; Boston, MA 02110-1301, USA. 22 | 23 | ;;; Commentary: 24 | 25 | ;; Place init-loader.el somewhere in your `load-path'. Then, add the 26 | ;; following lines to ~/.emacs or ~/.emacs.d/init.el: 27 | ;; 28 | ;; (require 'init-loader) 29 | ;; (init-loader-load "/path/to/init-directory") 30 | ;; 31 | ;; The last line loads configuration files in /path/to/init-directory. 32 | ;; If you omit arguments for `init-loader-load', the value of 33 | ;; `init-loader-directory' is used. 34 | ;; 35 | ;; Note that not all files in the directory are loaded. Each file is 36 | ;; examined that if it is a .el or .elc file and, it has a valid name 37 | ;; specified by `init-loader-default-regexp' or it is a platform 38 | ;; specific configuration file. 39 | ;; 40 | ;; By default, valid names of configuration files start with two 41 | ;; digits. For example, the following file names are all valid: 42 | ;; 00_util.el 43 | ;; 01_ik-cmd.el 44 | ;; 21_javascript.el 45 | ;; 99_global-keys.el 46 | ;; 47 | ;; Files are loaded in the lexicographical order. This helps you to 48 | ;; resolve dependency of the configurations. 49 | ;; 50 | ;; A platform specific configuration file has a prefix corresponds to 51 | ;; the platform. The following is the list of prefixes and platform 52 | ;; specific configuration files are loaded in the listed order after 53 | ;; non-platform specific configuration files. 54 | ;; 55 | ;; Platform Subplatform Prefix Example 56 | ;; ------------------------------------------------------------------------ 57 | ;; Windows windows- windows-fonts.el 58 | ;; Meadow meadow- meadow-commands.el 59 | ;; ------------------------------------------------------------------------ 60 | ;; Mac OS X Carbon Emacs carbon-emacs- carbon-emacs-applescript.el 61 | ;; Cocoa Emacs cocoa-emacs- cocoa-emacs-plist.el 62 | ;; ------------------------------------------------------------------------ 63 | ;; GNU/Linux linux- linux-commands.el 64 | ;; ------------------------------------------------------------------------ 65 | ;; *BSD bsd- bsd-commands.el 66 | ;; ------------------------------------------------------------------------ 67 | ;; All Non-window system nw- nw-key.el 68 | ;; 69 | ;; If `init-loader-byte-compile' is non-nil, each configuration file 70 | ;; is byte-compiled when it is loaded. If you modify the .el file, 71 | ;; then it is recompiled next time it is loaded. 72 | ;; 73 | ;; Loaded files and errors during the loading process are recorded. 74 | ;; If `init-loader-show-log-after-init' is `t', the record is 75 | ;; shown after the overall loading process. If `init-loader-show-log-after-init` 76 | ;; is `'error-only', the record is shown only error occured. 77 | ;; You can do this manually by M-x init-loader-show-log. 78 | ;; 79 | 80 | ;;; Code: 81 | 82 | (require 'cl-lib) 83 | (require 'benchmark) 84 | 85 | ;;; customize-variables 86 | (defgroup init-loader nil 87 | "Loader of configuration files." 88 | :prefix "init-loader-" 89 | :group 'initialization) 90 | 91 | (defcustom init-loader-directory 92 | (expand-file-name (concat (if (boundp 'user-emacs-directory) 93 | (file-name-as-directory user-emacs-directory) 94 | "~/.emacs.d/") 95 | "inits")) 96 | "Default directory of configuration files." 97 | :type 'directory) 98 | 99 | (defcustom init-loader-show-log-after-init t 100 | "Show loading log message if this value is t. If this value is `error-only', 101 | log message is shown only errors occured." 102 | :type 'boolean) 103 | 104 | (defcustom init-loader-byte-compile nil 105 | "Byte-compile configuration files if this value is non-nil." 106 | :type 'boolean) 107 | 108 | (defcustom init-loader-default-regexp "\\(?:\\`[[:digit:]]\\{2\\}\\)" 109 | "Regular expression determining valid configuration file names. 110 | 111 | The default value matches files that start with two digits. For 112 | example, 00_foo.el, 01_bar.el ... 99_keybinds.el." 113 | :type 'regexp) 114 | 115 | (defcustom init-loader-meadow-regexp "\\`meadow-" 116 | "Regular expression of Meadow specific configuration file names." 117 | :type 'regexp) 118 | 119 | (defcustom init-loader-windows-regexp "\\`windows-" 120 | "Regular expression of Windows specific configuration file names." 121 | :type 'regexp) 122 | 123 | (defcustom init-loader-carbon-emacs-regexp "\\`carbon-emacs-" 124 | "Regular expression of Carbon Emacs specific configuration file names." 125 | :type 'regexp) 126 | 127 | (defcustom init-loader-cocoa-emacs-regexp "\\`cocoa-emacs-" 128 | "Regular expression of Cocoa Emacs specific configuration file names." 129 | :type 'regexp) 130 | 131 | (defcustom init-loader-nw-regexp "\\`nw-" 132 | "Regular expression of no-window Emacs configuration file names." 133 | :type 'regexp) 134 | 135 | (defcustom init-loader-linux-regexp "\\`linux-" 136 | "Regular expression of GNU/Linux specific configuration file names." 137 | :type 'regexp) 138 | 139 | (defcustom init-loader-bsd-regexp "\\`bsd-" 140 | "Regular expression of *BSD specific configuration file names." 141 | :type 'regexp) 142 | 143 | ;;;###autoload 144 | (cl-defun init-loader-load (&optional (init-dir init-loader-directory)) 145 | "Load configuration files in INIT-DIR." 146 | (let ((init-dir (init-loader-follow-symlink init-dir)) 147 | (is-carbon-emacs nil)) 148 | (cl-assert (and (stringp init-dir) (file-directory-p init-dir))) 149 | (init-loader-re-load init-loader-default-regexp init-dir t) 150 | 151 | ;; Windows 152 | (when (featurep 'dos-w32) 153 | (init-loader-re-load init-loader-windows-regexp init-dir)) 154 | ;; meadow 155 | (when (featurep 'meadow) 156 | (init-loader-re-load init-loader-meadow-regexp init-dir)) 157 | 158 | ;; Carbon Emacs 159 | (when (featurep 'carbon-emacs-package) 160 | (init-loader-re-load init-loader-carbon-emacs-regexp init-dir) 161 | (setq is-carbon-emacs t)) 162 | ;; Cocoa Emacs 163 | (when (or (memq window-system '(ns mac)) 164 | (and (not is-carbon-emacs) ;; for daemon mode 165 | (not window-system) 166 | (eq system-type 'darwin))) 167 | (init-loader-re-load init-loader-cocoa-emacs-regexp init-dir)) 168 | 169 | ;; GNU Linux 170 | (when (eq system-type 'gnu/linux) 171 | (init-loader-re-load init-loader-linux-regexp init-dir)) 172 | 173 | ;; *BSD 174 | (when (eq system-type 'berkeley-unix) 175 | (init-loader-re-load init-loader-bsd-regexp init-dir)) 176 | 177 | ;; no-window 178 | (when (not window-system) 179 | (init-loader-re-load init-loader-nw-regexp init-dir)) 180 | 181 | (cl-case init-loader-show-log-after-init 182 | (error-only (add-hook 'after-init-hook 'init-loader--show-log-error-only)) 183 | ('t (add-hook 'after-init-hook 'init-loader-show-log))))) 184 | 185 | (defun init-loader-follow-symlink (dir) 186 | (cond ((file-symlink-p dir) 187 | (expand-file-name (file-symlink-p dir))) 188 | (t (expand-file-name dir)))) 189 | 190 | (defvar init-loader--log-buffer nil) 191 | (defun init-loader-log (&optional msg) 192 | (if msg 193 | (when (stringp msg) 194 | (push msg init-loader--log-buffer)) 195 | (mapconcat 'identity (reverse init-loader--log-buffer) "\n"))) 196 | 197 | (defvar init-loader--error-log-buffer nil) 198 | (defun init-loader-error-log (&optional msg) 199 | (if msg 200 | (when (stringp msg) 201 | (push msg init-loader--error-log-buffer)) 202 | (mapconcat 'identity (reverse init-loader--error-log-buffer) "\n"))) 203 | 204 | (defvar init-loader-before-compile-hook nil) 205 | (defun init-loader-load-file (file) 206 | (when init-loader-byte-compile 207 | (let* ((path (file-name-sans-extension (locate-library file))) 208 | (el (concat path ".el")) (elc (concat path ".elc"))) 209 | (when (and (not (file-exists-p el)) (file-exists-p elc)) 210 | (error "There is only byte-compiled file.")) 211 | (when (or (not (file-exists-p elc)) 212 | (file-newer-than-file-p el elc)) 213 | (when (file-exists-p elc) (delete-file elc)) 214 | (run-hook-with-args 'init-loader-before-compile-hook file) 215 | (byte-compile-file el)))) 216 | (load file)) 217 | 218 | (defun init-loader-re-load (re dir &optional sort) 219 | ;; 2011/JUN/12 zqwell: Don't localize `load-path' and use it as global 220 | (add-to-list 'load-path dir) 221 | (dolist (el (init-loader--re-load-files re dir sort)) 222 | (condition-case e 223 | (let ((time (car (benchmark-run (init-loader-load-file (file-name-sans-extension el)))))) 224 | (init-loader-log (format "loaded %s. %s" (locate-library el) time))) 225 | (error 226 | ;; 2011/JUN/12 zqwell: Improve error message 227 | ;; See. http://d.hatena.ne.jp/kitokitoki/20101205/p1 228 | (init-loader-error-log (format "%s. %s" (locate-library el) (error-message-string e))))))) 229 | 230 | ;; 2011/JUN/12 zqwell Read first byte-compiled file if it exist. 231 | ;; See. http://twitter.com/#!/fkmn/statuses/21411277599 232 | (defun init-loader--re-load-files (re dir &optional sort) 233 | (cl-loop for el in (directory-files dir t) 234 | when (and (string-match re (file-name-nondirectory el)) 235 | (or (string-match "elc\\'" el) 236 | (and (string-match "el\\'" el) 237 | (not (locate-library (concat el "c")))))) 238 | collect (file-name-nondirectory el) into ret 239 | finally return (if sort (sort ret 'string<) ret))) 240 | 241 | (defun init-loader--show-log-error-only () 242 | (let ((err (init-loader-error-log))) 243 | (when (and err (not (string= err ""))) 244 | (init-loader-show-log)))) 245 | 246 | ;;;###autoload 247 | (defun init-loader-show-log () 248 | "Show init-loader log buffer." 249 | (interactive) 250 | (let ((b (get-buffer-create "*init log*"))) 251 | (with-current-buffer b 252 | (view-mode -1) 253 | (erase-buffer) 254 | (insert "------- error log -------\n\n" 255 | (init-loader-error-log) 256 | "\n\n") 257 | (insert "------- init log -------\n\n" 258 | (init-loader-log) 259 | "\n\n") 260 | ;; load-path 261 | (insert "------- load path -------\n\n" 262 | (mapconcat 'identity load-path "\n")) 263 | (goto-char (point-min))) 264 | (switch-to-buffer b) 265 | (view-mode +1))) 266 | 267 | (provide 'init-loader) 268 | 269 | ;; Local Variables: 270 | ;; coding: utf-8 271 | ;; indent-tabs-mode: nil 272 | ;; End: 273 | 274 | ;;; init-loader.el ends here 275 | -------------------------------------------------------------------------------- /test-init-loader.el: -------------------------------------------------------------------------------- 1 | ;;; test-init-loader.el --- Test for init-loader.el 2 | 3 | ;; Copyright (C) 2013 by emacs-jp 4 | 5 | ;; This program is free software; you can redistribute it and/or modify 6 | ;; it under the terms of the GNU General Public License as published by 7 | ;; the Free Software Foundation, either version 3 of the License, or 8 | ;; (at your option) any later version. 9 | 10 | ;; This program is distributed in the hope that it will be useful, 11 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | ;; GNU General Public License for more details. 14 | 15 | ;; You should have received a copy of the GNU General Public License 16 | ;; along with this program. If not, see . 17 | 18 | ;;; Commentary: 19 | 20 | ;;; Code: 21 | 22 | (require 'cl-lib) 23 | (require 'ert) 24 | (require 'init-loader) 25 | 26 | (defvar init-loader-test-files 27 | '("00_utils.el" 28 | "23_yaml.el" 29 | "01_ik-cmd.el" 30 | "96_color.el" 31 | "20_elisp.el" 32 | "21_javascript.el" 33 | "25_perl.el" 34 | "98_emacs-config.el" 35 | "99_global-keys.el" 36 | "carbon-emacs-config.el" 37 | "carbon-emacs-migemo.el" 38 | "nw-config.el" 39 | "emacs-migemo.el" 40 | "windows-powershell.el" 41 | "windows-fonts.el" 42 | "meadow-cmd.el" 43 | "meadow-config.el" 44 | "meadow-gnuserv.el" 45 | "meadow-shell.el" 46 | "meadow-w32-symlinks.el" 47 | "linux-fonts.el" 48 | "linux-commands.el" 49 | "bsd-config.el" 50 | "bsd-migemo.el")) 51 | 52 | (ert-deftest init-loader--re-load-files () 53 | "Test for `init-loader--re-load-files'" 54 | (cl-letf (((symbol-function #'directory-files) 55 | (lambda (dir &optional full match nosort) 56 | init-loader-test-files))) 57 | (let ((got (init-loader--re-load-files init-loader-default-regexp "" t)) 58 | (expected '("00_utils.el" "01_ik-cmd.el" "20_elisp.el" 59 | "21_javascript.el" "23_yaml.el" "25_perl.el" 60 | "96_color.el" "98_emacs-config.el" "99_global-keys.el"))) 61 | (should (equal got expected))) 62 | 63 | (let ((got (init-loader--re-load-files init-loader-windows-regexp "" t)) 64 | (expected '("windows-fonts.el" "windows-powershell.el"))) 65 | (should (equal got expected))) 66 | 67 | (let ((got (init-loader--re-load-files init-loader-meadow-regexp "" t)) 68 | (expected '("meadow-cmd.el" "meadow-config.el" "meadow-gnuserv.el" 69 | "meadow-shell.el" "meadow-w32-symlinks.el"))) 70 | (should (equal got expected))) 71 | 72 | (let ((got (init-loader--re-load-files init-loader-carbon-emacs-regexp "" t)) 73 | (expected '("carbon-emacs-config.el" "carbon-emacs-migemo.el"))) 74 | (should (equal got expected))) 75 | 76 | (let ((got (init-loader--re-load-files init-loader-linux-regexp "" t)) 77 | (expected '("linux-commands.el" "linux-fonts.el"))) 78 | (should (equal got expected))) 79 | 80 | (let ((got (init-loader--re-load-files init-loader-bsd-regexp "" t)) 81 | (expected '("bsd-config.el" "bsd-migemo.el"))) 82 | (should (equal got expected))) 83 | 84 | (let ((got (init-loader--re-load-files init-loader-nw-regexp "" t)) 85 | (expected '("nw-config.el"))) 86 | (should (equal got expected))) 87 | 88 | ;; accept '.elc' files 89 | (push "nw-added.elc" init-loader-test-files) 90 | (let ((got (init-loader--re-load-files init-loader-nw-regexp "" t)) 91 | (expected '("nw-added.elc" "nw-config.el"))) 92 | (should (equal got expected))) 93 | (pop init-loader-test-files))) 94 | 95 | (ert-deftest init-loader-follow-symlink () 96 | "Test for `init-loader-follow-symlink'" 97 | (cl-letf (((symbol-function #'directory-files) 98 | (lambda (dir &optional full match nosort) 99 | init-loader-test-files))) 100 | (let ((symlink "symlink.el") 101 | (thisfile "test-init-loader.el")) 102 | ;; setup 103 | (make-symbolic-link thisfile symlink t) 104 | 105 | ;; symbolic link 106 | (let ((expected (expand-file-name (concat default-directory thisfile)))) 107 | (should (string= (init-loader-follow-symlink symlink) expected))) 108 | 109 | ;; not symbolic link 110 | (let ((expected (getenv "HOME"))) 111 | (should (string= (init-loader-follow-symlink "~") expected))) 112 | 113 | ;; teardown 114 | (delete-file symlink)))) 115 | 116 | (ert-deftest init-loader-load () 117 | "Test `init-loader-load'" 118 | ;; Test `init-loader-show-log-after-init' switch. 119 | (cl-letf (((symbol-function #'directory-files) 120 | (lambda (dir &optional full match nosort) 121 | init-loader-test-files))) 122 | (let ((init-loader-show-log-after-init t) 123 | (after-init-hook nil)) 124 | (init-loader-load "") 125 | (should 126 | (and (member 'init-loader-show-log after-init-hook) 127 | (not (member 'init-loader--show-log-error-only after-init-hook))))) 128 | 129 | (let ((init-loader-show-log-after-init 'error-only) 130 | (after-init-hook nil)) 131 | (init-loader-load "") 132 | (should 133 | (and (not (member 'init-loader-show-log after-init-hook)) 134 | (member 'init-loader--show-log-error-only after-init-hook)))) 135 | 136 | (let ((init-loader-show-log-after-init nil) 137 | (after-init-hook nil)) 138 | (init-loader-load "") 139 | (should 140 | (and (not (member 'init-loader-show-log after-init-hook)) 141 | (not (member 'init-loader--show-log-error-only after-init-hook))))))) 142 | 143 | (ert-deftest init-loader-log () 144 | "Test for `init-loader-log'" 145 | ;; pass not string value 146 | (should-not (init-loader-log 1)) 147 | 148 | ;; log message 149 | (init-loader-log "message1") 150 | (should (string= "message1" (init-loader-log))) 151 | 152 | ;; log message again 153 | (init-loader-log "message2") 154 | (should (string= "message1\nmessage2" (init-loader-log)))) 155 | 156 | (ert-deftest init-loader-error-log () 157 | "Test for `init-loader-error-log'" 158 | ;; pass not string value 159 | (should-not (init-loader-error-log 1)) 160 | 161 | ;; log message 162 | (init-loader-error-log "message1") 163 | (should (string= "message1" (init-loader-error-log))) 164 | 165 | ;; log message again 166 | (init-loader-error-log "message2") 167 | (should (string= "message1\nmessage2" (init-loader-error-log)))) 168 | 169 | (defvar is-byte-compiled nil) 170 | (defvar is-loaded nil) 171 | (defvar is-deleted nil) 172 | 173 | (defun test-init-loader-clear-flags () 174 | (setq is-byte-compiled nil is-loaded nil is-deleted nil)) 175 | 176 | (ert-deftest init-loader-load-file () 177 | "Test for `init-loader-load-file'" 178 | 179 | (cl-letf (((symbol-function #'byte-compile-file) 180 | (lambda (el) 181 | (setq is-byte-compiled t))) 182 | ((symbol-function #'load) 183 | (lambda (file &optional noerror nomessage nosuffix must-suffix) 184 | (setq is-loaded t))) 185 | ((symbol-function #'delete-file) 186 | (lambda (file &optional trash) 187 | (setq is-deleted t))) 188 | ((symbol-function #'locate-library) 189 | (lambda (lib &optional nosuffix path interactive-call) 190 | lib))) 191 | ;; not byte compile 192 | (let ((init-loader-byte-compile nil)) 193 | (init-loader-load-file "foo") 194 | (should (and (not is-byte-compiled) is-loaded))) 195 | 196 | (test-init-loader-clear-flags) 197 | 198 | (let ((init-loader-byte-compile t)) 199 | (init-loader-load-file "foo") 200 | (should (and is-byte-compiled (not is-deleted) 201 | is-loaded))) 202 | 203 | (test-init-loader-clear-flags) 204 | 205 | (let ((init-loader-byte-compile t)) 206 | ;; .elc file is older than .el file 207 | (shell-command "touch foo.elc ; sleep 1") 208 | (shell-command "touch foo.el") 209 | 210 | (init-loader-load-file "foo") 211 | (should (and is-byte-compiled is-deleted is-loaded)) 212 | 213 | (shell-command "rm -f foo.el foo.elc")))) 214 | 215 | ;;; test-init-loader.el end here 216 | --------------------------------------------------------------------------------