diff --git a/.github/workflows/integration_tests.yml b/.github/workflows/integration_tests.yml index 29ed436db..1a2083870 100644 --- a/.github/workflows/integration_tests.yml +++ b/.github/workflows/integration_tests.yml @@ -3,20 +3,7 @@ on: [push, pull_request] jobs: - initialize-repository: - runs-on: ubuntu-latest - defaults: - run: - shell: bash - - steps: - - name: Checkout repository - uses: actions/checkout@v4 - with: - fetch-depth: 1 - integration-tests: - needs: initialize-repository runs-on: ubuntu-latest steps: diff --git a/database/migrate_legacy_data_20220101.sh b/database/migrate_legacy_data_20220101.sh index 8de785997..3082fe063 100755 --- a/database/migrate_legacy_data_20220101.sh +++ b/database/migrate_legacy_data_20220101.sh @@ -99,7 +99,7 @@ function migrate_statistics() cmd_manager 'SILENT' "mv ${datadir}/statistics ${datadir}/legacy_statistics" if [[ "$?" != 0 ]]; then complain "Couldn't rename ${datadir}/statistics ${datadir}/legacy_statistics" - return 1 #EPERM + return 1 # EPERM fi } @@ -161,7 +161,7 @@ function migrate_pomodoro() cmd_manager 'SILENT' "mv ${datadir}/pomodoro ${datadir}/legacy_pomodoro" if [[ "$?" != 0 ]]; then complain "Couldn't rename ${datadir}/pomodoro ${datadir}/legacy_pomodoro" - return 1 #EPERM + return 1 # EPERM fi } @@ -216,20 +216,20 @@ function migrate_kernel_configs() cmd_manager 'SILENT' "cp -r ${configs_dir}/. ${datadir}/configs" if [[ "$?" != 0 ]]; then complain "Couldn't copy kernel config files from ${configs_dir} to ${datadir}/configs" - return 1 #EPERM + return 1 # EPERM fi # mark migrated directories to avoid duplicated data cmd_manager 'SILENT' "mv ${datadir}/configs/configs ${datadir}/configs/legacy_configs" if [[ "$?" != 0 ]]; then complain "Couldn't rename ${datadir}/configs/configs to ${datadir}/configs/legacy_configs" - return 1 #EPERM + return 1 # EPERM fi cmd_manager 'SILENT' "mv ${datadir}/configs/metadata ${datadir}/configs/legacy_metadata" if [[ "$?" != 0 ]]; then complain "Couldn't rename ${datadir}/configs/metadata to ${datadir}/configs/legacy_metadata" - return 1 #EPERM + return 1 # EPERM fi } diff --git a/documentation/dependencies/kernel_build/arch.dependencies b/documentation/dependencies/kernel_build/arch.dependencies new file mode 100644 index 000000000..9bbd5ff01 --- /dev/null +++ b/documentation/dependencies/kernel_build/arch.dependencies @@ -0,0 +1,17 @@ +make +base-devel +linux-headers +bison +flex +openssl +ncurses +pahole +xmlto +kmod +inetutils +bc +libelf +cpio +perl +tar +xz diff --git a/documentation/dependencies/kernel_build/debian.dependencies b/documentation/dependencies/kernel_build/debian.dependencies new file mode 100644 index 000000000..5389331a8 --- /dev/null +++ b/documentation/dependencies/kernel_build/debian.dependencies @@ -0,0 +1,20 @@ +libncurses-dev +gawk +flex +bison +openssl +libssl-dev +dkms +libelf-dev +libudev-dev +libpci-dev +libiberty-dev +autoconf +llvm +exuberant-ctags +build-essential +xmlto +kmod +dwarves +clang +lld diff --git a/documentation/dependencies/kernel_build/fedora.dependencies b/documentation/dependencies/kernel_build/fedora.dependencies new file mode 100644 index 000000000..4b93e530d --- /dev/null +++ b/documentation/dependencies/kernel_build/fedora.dependencies @@ -0,0 +1,12 @@ +qt3-devel +libXi-devel +diffutils +gcc-c++ +ncurses-devel +bison +flex +openssl-devel +elfutils-libelf-devel +openssl +dwarves +llvm diff --git a/documentation/man/features/kw-build.rst b/documentation/man/features/kw-build.rst index 8fd82b455..085c1e514 100644 --- a/documentation/man/features/kw-build.rst +++ b/documentation/man/features/kw-build.rst @@ -17,6 +17,7 @@ SYNOPSIS | *kw* (*b* | *build*) [(-c | \--clean)] [\--alert=(s | v | (sv | vs) | n)] | *kw* (*b* | *build*) [(-f | \--full-cleanup)] [\--alert=(s | v | (sv | vs) | n)] | *kw* (*b* | *build*) [\--cflags] +| *kw* (*b* | *build*) [\--from-sha ] | *kw* (*b* | *build*) [\--verbose] DESCRIPTION @@ -40,7 +41,7 @@ OPTIONS -n, \--menu: The menu option invokes the kernel menuconfig. Notice that the default menu - config can be changed in the **kworkflow.config** file by setting a different + config can be changed in the **build.config** file by setting a different option in *menu_config*. If the user is working in a *cross-compile* environment, it is recommended to use this option to avoid messing with the config file manually. @@ -48,7 +49,7 @@ OPTIONS -d, \--doc: The doc option provides a mechanism for building the kernel-doc; by default, it will build htmldocs. Users can change the default documentation output by - changing the parameter *doc_type* in the **kworkflow.config** file. + changing the parameter *doc_type* in the **build.config** file. -S, \--cpu-scaling: The cpu-scaling option lets the user set whichever CPU usage they want from @@ -61,12 +62,12 @@ OPTIONS -w, \--warnings (1 | 2 | 3 | 12 | 13 | 23 | 123): This can be used to enable compilation warnings accordingly. You can set the - default log level via `build.config` file under the option `warning_level`. + default log level via **build.config** file under the option `warning_level`. Please check the kernel's ``make help`` for more info. --s, \--save-log-to=path: +-s, \--save-log-to path: This option will save the full compilation log with the enabled warnings to - the specified path. You can set the default log path in the `build.config` + the specified path. You can set the default log path in the **build.config** file via `log_path` option. \--llvm: @@ -102,10 +103,15 @@ OPTIONS | **sv** or **vs** enables both. | **n** (or any other option) disables notifications (this is the default). +\--from-sha: + Build every commit after to branch head. Useful for testing if all patches in + patchset compile. + EXAMPLES ======== -For these examples, we suppose the fields in your **kworkflow.config** file are -already configured. +For these examples, we assume that the relevant fields in your configuration +files (located by default in **.kw/**) have already been setup. We recommend +the use of ``kw config`` for managing your local and global configurations. For building and installing a new module version based on the current kernel version, you can use:: @@ -141,7 +147,7 @@ using:: Sometimes we have a lot of error message that does not fit in the terminal buffer; in these cases it is helpful to save all logs in a file:: - kw b --warnings 123 --save-log-to=ALL_WARNINGS.log + kw b --warnings 123 --save-log-to ALL_WARNINGS.log If you want to use llvm:: @@ -158,3 +164,11 @@ If you want to reset the kernel tree to its default, `all config and script outp If you want to use cflags:: kw b --cflags "-O3 -pipe -march=native" + +If you want to build every commit after HEAD~2 to HEAD:: + + kw b --from-sha HEAD~2 + +If you want to build every commit after ee3b5 to HEAD:: + + kw b --from-sha ee3b5 diff --git a/documentation/man/features/kw-codestyle.rst b/documentation/man/features/kw-codestyle.rst index 02d3cfab5..c6b9cd985 100644 --- a/documentation/man/features/kw-codestyle.rst +++ b/documentation/man/features/kw-codestyle.rst @@ -29,8 +29,9 @@ OPTIONS EXAMPLES ======== -For these examples, we suppose the fields in your **kworkflow.config** file are -already configured. +For these examples, we assume that the relevant fields in your configuration +files (located by default in **.kw/**) have already been setup. We recommend +the use of ``kw config`` for managing your local and global configurations. For checking the code style:: diff --git a/documentation/man/features/kw-deploy.rst b/documentation/man/features/kw-deploy.rst index 096f84f0a..116cda646 100644 --- a/documentation/man/features/kw-deploy.rst +++ b/documentation/man/features/kw-deploy.rst @@ -31,22 +31,23 @@ executed: You can specify the deploy target via command line by using the flag ``--remote :`` (e.g., ``--remote 172.16.254.1:22``); however, if -you do this frequently you will probably prefer to add this information to your -local **kworkflow.config**. See the example below:: +you plan on deploying to the same remote frequently can benefit from using the +``kw remote`` feature to save the SSH information in a configuration file +used by kw, for example:: - default_deploy_target=remote - ssh_user=root - ssh_ip=172.16.254.1 - ssh_port=22 + kw remote --add origin root@172.16.254.1 + +For more information, check ``kw remote --help`` If you want to install a new kernel version in your host machine, you can use the flag ``--local``; you will need to use your root password. -Another typical operation when deploying a new kernel to a test machine, it is -the reboot after the update. You can explicitly say it for **kw** by adding the -flag ``--reboot``, add this to the **kworkflow.config** with:: +Another typical operation when deploying a new kernel to a test machine is +rebooting after the update. You can add the ``--reboot`` flag to a command to +explicitly make **kw** reboot the machine afterwards, or you can set this to +always happen by modifying ``reboot_after_deploy`` flag in **deploy.config** with:: - reboot_after_deploy=yes + kw config deploy.reboot_after_deploy yes This can be used with conjunction the :ref:`build` command by invoking ``kw bd``. @@ -117,8 +118,9 @@ OPTIONS EXAMPLES ======== -For these examples, we suppose the fields in your **kworkflow.config** file are -already configured. +For these examples, we assume that the relevant fields in your configuration +files (located by default in **.kw/**) have already been setup. We recommend +the use of ``kw config`` for managing your local and global configurations. First, if you are working in a specific kernel module, and if you want to install your recent changes in your local machine you can use:: diff --git a/documentation/man/features/kw-drm.rst b/documentation/man/features/kw-drm.rst index 3368d269a..4fbddaadb 100644 --- a/documentation/man/features/kw-drm.rst +++ b/documentation/man/features/kw-drm.rst @@ -26,8 +26,8 @@ OPTIONS \--local, \--remote [:]: This option specifies the target device for the drm action, it can be a remote or local machine. If these options are not explicitly passed via - command line, **kw** going to take the target set in the variable - *default_deploy_target* (**kworkflow.config**) for identifying the target. + command line, **kw** is going to take the target set in the variable + *default_deploy_target* (**deploy.config**) for identifying the target. It is important to highlight that the drm feature **does not support VM**. -lm, \--load-module=[:,...][;:...]: @@ -55,8 +55,25 @@ OPTIONS systemctl operation but users can change this behavior by set *gui_off* with a specific command in the **kworkflow.config** file with the specific command. +\--gui-on-after-reboot: + This option sets the GUI to turn on automatically after the next reboot. By default, + it uses systemctl operation; however, users are free to add any specific + command for activating their preferred GUI in the variable *gui_on_after_reboot* + in the **kworkflow.config** file. + Note: This change will take effect only after the system is rebooted. To enable + the GUI immediately, use the --gui-on option. + +\--gui-off-after-reboot: + This option sets the GUI to turn off automatically after the next reboot. By default, + it uses systemctl operation; however, users are free to add any specific + command for deactivating their GUI in the variable *gui_off_after_reboot* + in the **kworkflow.config** file. + Note: This change will take effect only after the system is rebooted. To disable + the GUI immediately, use the --gui-off option. + \--conn-available: - Show all connectors available in the target machine. + Show all connectors available in the target machine. The ones marked with '*' + are enabled. \--modes: Show all available modes per card. @@ -68,8 +85,9 @@ OPTIONS EXAMPLES ======== -For these examples, we suppose the fields in your **kworkflow.config** file are -already configured. +For these examples, we assume that the relevant fields in your configuration +files (located by default in **.kw/**) have already been setup. We recommend +the use of ``kw config`` for managing your local and global configurations. If you are working with DRM drivers, you can take advantage of load and unload commands combined with GUI control commands. For example:: diff --git a/documentation/man/features/kw-explore.rst b/documentation/man/features/kw-explore.rst index 5138c3bce..0f5a28aa6 100644 --- a/documentation/man/features/kw-explore.rst +++ b/documentation/man/features/kw-explore.rst @@ -7,7 +7,7 @@ kw-explore - Explore folder SYNOPSIS ======== *kw* (*e* | *explore*) [(-l | \--log) | (-g | \--grep) | (-a | \--all) | \--verbose] - [(-c | \--only-source) | (-H | \--only-header)] + [(-c | \--only-source) | (-H | \--only-header)] [(-C[] | \--show-context[=])] [-p] [ | ] DESCRIPTION @@ -45,5 +45,23 @@ OPTIONS -H | \--only-header: With this option, it is possible to show only the results from the header. +-C[] | \--show-context[=]: + Show lines of additional context above and below the matched line. + If is not specified, the default value of 3 will be used. + \--verbose: Verbose mode allows the user to see the commands executed under the hood. + +EXAMPLES +======== +To show matched line with context using long-form flag:: + + kw explore --show-context=5 search_string + +To show matched line with context using short flag:: + + kw explore -C5 search_string + +Search through all tracked and untracked files, default value of 3 will be used for context:: + + kw explore -C --all search_string diff --git a/documentation/man/features/kw-init.rst b/documentation/man/features/kw-init.rst index 8d85b095a..4b6770133 100644 --- a/documentation/man/features/kw-init.rst +++ b/documentation/man/features/kw-init.rst @@ -11,9 +11,9 @@ SYNOPSIS DESCRIPTION =========== -This command creates a **.kw** folder containing a **kworkflow.config** file in +This command creates a **.kw** folder containing default configuration files in the current kernel directory. The primary reason for running ``kw init`` is to -pick up a freshly created config file. +pick up freshly created config files. OPTIONS ======= @@ -25,7 +25,7 @@ OPTIONS creating the local config file. \--arch : - Set the variable `arch` from the newly created **kworkflow.config** file. + Set the variable `arch` from the newly created **build.config** file. Before actually changing it, this option checks if ** is a valid architecture found in the **arch** folder from the kernel directory. @@ -34,7 +34,7 @@ OPTIONS and **, respectively. \--target : - Set the variable `default_deploy_target` from **kworkflow.config** to + Set the variable `default_deploy_target` from **deploy.config** to **, which can be local or remote. \--verbose: @@ -45,17 +45,19 @@ EXAMPLES For these examples, we suppose that the kernel directory is your current directory. -For initializing a **kworkflow.config** with `arch` set to arm, use:: +For initializing a **build.config** with `arch` set to arm, use:: kw init --arch arm -To initialize **kworkflow.config** with `arch` set to x86, `ssh_user` set to -john, `ssh_ip` set to localhost, and `ssh_port` set to 2222, run:: +To initialize **build.config** with `arch` set to x86 and a **kworkflow.config** +with `ssh_user` set to john, `ssh_ip` set to localhost, and `ssh_port` +set to 2222, run:: kw init --arch x86 --remote john@localhost:2222 -For initializing a **kworkflow.config** with `arch` set to arm64, `target` set to -remote, `ssh_user` mary, `ssh_ip` localhost, and `ssh_port` 1234, run:: +For initializing a **build.config** with `arch` set to arm64, a **deploy.config** +with `target` set to remote and a **kworkflow.config** with `ssh_user` mary, +`ssh_ip` localhost, and `ssh_port` 1234, run:: kw init --arch arm64 --remote mary@localhost:1234 --target remote diff --git a/documentation/man/features/kw-kernel-config-manager.rst b/documentation/man/features/kw-kernel-config-manager.rst index 0c4a13595..404da4e75 100644 --- a/documentation/man/features/kw-kernel-config-manager.rst +++ b/documentation/man/features/kw-kernel-config-manager.rst @@ -62,8 +62,9 @@ OPTIONS EXAMPLES ======== -In the following examples, we assume your **kworkflow.config** file is already -properly configured. +For these examples, we assume that the relevant fields in your configuration +files (located by default in **.kw/**) have already been setup. We recommend +the use of ``kw config`` for managing your local and global configurations. In case you want **kw** to save your current **.config** file, you can use:: diff --git a/documentation/man/features/kw-mail.rst b/documentation/man/features/kw-mail.rst index 62f3429ad..c4fb3a633 100644 --- a/documentation/man/features/kw-mail.rst +++ b/documentation/man/features/kw-mail.rst @@ -29,12 +29,12 @@ the union of the recipients of each patch as the recipients of the cover-letter. .. note:: You can block certain e-mail addresses from being automatically added to the recipients list of the patches using the *blocked_emails* option in the - *kworkflow.config* file. + **mail.config** file. .. note:: You can add To\: and CC\: recipients to be included by default using the *default_to_recipients* and *default_cc_recipients* configurations, respectively, - in the *mail.config* file. + in the **mail.config** file. .. note:: Any option recognized by ``git send-email`` can be passed directly to it if @@ -53,7 +53,7 @@ OPTIONS .. note:: You can change the default arguments used to send emails in the - *kworkflow.config* file. + **mail.config** file. \--to=',...': Specify the recipients that will receive the patch via e-mail. The diff --git a/documentation/man/features/kw-maintainers.rst b/documentation/man/features/kw-maintainers.rst index e44347bf7..59017a86e 100644 --- a/documentation/man/features/kw-maintainers.rst +++ b/documentation/man/features/kw-maintainers.rst @@ -36,8 +36,9 @@ OPTIONS EXAMPLES ======== -For these examples, we suppose the fields in your **kworkflow.config** file are -already configured. +For these examples, we assume that the relevant fields in your configuration +files (located by default in **.kw/**) have already been setup. We recommend +the use of ``kw config`` for managing your local and global configurations. If you want to check the maintainers:: diff --git a/documentation/man/features/kw-ssh.rst b/documentation/man/features/kw-ssh.rst index 7ca708fac..4a76f3d36 100644 --- a/documentation/man/features/kw-ssh.rst +++ b/documentation/man/features/kw-ssh.rst @@ -51,8 +51,9 @@ OPTIONS EXAMPLES ======== -For these examples, we suppose the fields in your **kworkflow.config** file are -already configured. +For these examples, we assume that the relevant fields in your configuration +files (located by default in **.kw/**) have already been setup. We recommend +the use of ``kw config`` for managing your local and global configurations. After you start your VM you can ssh into it with:: diff --git a/documentation/man/features/kw-vm.rst b/documentation/man/features/kw-vm.rst index fb78e9dd9..8b866bdd9 100644 --- a/documentation/man/features/kw-vm.rst +++ b/documentation/man/features/kw-vm.rst @@ -21,7 +21,7 @@ OPTIONS ======= -m, \--mount: This mounts the QEMU image in a specific directory, based on the data available - in the **kworkflow.config** file. Notice that the configuration file has the + in the **vm.config** file. Notice that the configuration file has the information about the source image and destination mount point. .. note:: @@ -29,10 +29,10 @@ OPTIONS -n, \--umount: This unmounts the previously mounted QEMU image, based on the parameters - available in the **kworkflow.config** file. + available in the **vm.config** file. -u, \--up: - This starts the QEMU VM based on parameters in the **kworkflow.config** file. + This starts the QEMU VM based on parameters in the **vm.config** file. \--alert=(s | v | (sv | vs) | n): Defines the alert behaviour upon the command completion. diff --git a/documentation/man/kw.rst b/documentation/man/kw.rst index 0f2190505..fca6e7608 100644 --- a/documentation/man/kw.rst +++ b/documentation/man/kw.rst @@ -19,8 +19,8 @@ DESCRIPTION =========== **kw** mission is: reduce the overhead related with infrastructure project setup in projects that have a similar workflow to the Linux Kernel. It can (and -should) be customized by editing the **kworkflow.config** file, as discussed in -section `ABOUT kworflow.config`_. +should) be customized by editing its various configuration files (preferably +through the **kw config** command). COMMANDS ======== @@ -88,6 +88,7 @@ This section describes a tool available in **kw** to help developers keep track of configuration files and other features provided by **kw** that do not fit in the previous sections. + | :ref:`kw-config` | :ref:`kw-backup` | :ref:`kw-init` | :ref:`kw-device` @@ -111,108 +112,11 @@ version, \--version, -v ~~~~~~~~~~~~~~~~~~~~~~~ Show kworkflow version. -ABOUT kworflow.config -===================== -.. _`ABOUT kworkflow.config`: - -**kw** reads its configuration from two files: the global -*/etc/kworkflow.config* file and the local **kworkflow.config** file -present at the current working directory. The global **kworkflow.config** is a -part of the **kw** code and provides the overall behavior for **kw**. Local -**kworkflow.config** settings override global ones; you may have one -**kworkflow.config** per project. In this section, we describe the possible -fields you can specify in the configuration files. - -ssh_user= ---------------- -Sets the user to be used by ssh. By default **kw** uses ``root``. - -ssh_ip= ------------ -Sets the IP address to be used by ssh. By default **kw** uses ``localhost``. - -ssh_port= ---------------- -Sets the ssh port. By default **kw** uses ``2222``. - -ssh_configfile= --------------------------------- -Provides an optional SSH configuration file to be used by ssh. For more details -see ``man ssh_config``. - -hostname= -------------------- -Sets the hostname to be used when an SSH configuration file is provided. - -arch= -------------------- -Allows you to specify the default architecture used by **kw**. By default, -**kw** uses ``x86_64``. - -kernel_img_name= ------------------------------------ -Use this option as a way to indicate to kw the kernel image name. This is the -file present in the directory ``arch/*/boot/``; keep in mind that the kernel -image name might change based on the user config file or target architecture. - -cross_compile= --------------------------------------------- -Kw supports cross compile setup, use this option to indicate the target -toolchain. - -menu_config= -------------------------- -Default kernel menu used by **kw**, the default is ``nconfig``. - -alert=[vs | s | v | n] ----------------------- -Default alert options, you have: - -1. v: enables visual notification. - -2. s: enables sound notification. - -3. vs or sv: enables both. - -4. n (or any other option): disables notifications. - -sound_alert_command= ------------------------------ -Command to run for sound completion alert. By default, **kw** uses -``paplay INSTALLPATH/sounds/complete.wav &`` - -visual_alert_command= ------------------------------- -Command to run for visual completion alert. By default, **kw** uses -``notify-send -i checkbox -t 10000 "kw" "Command: \\"$COMMAND\\" completed!"`` - -.. note:: - You may use the *COMMAND* variable, which will be replaced by the kw command - whose conclusion the user wished to be alerted of. - -default_deploy_target ---------------------- -By default, **kw** deploys in the *remote*; however, you can change this -behavior with this variable. The available options are: *local* and *remote*. - -reboot_remote_by_default ------------------------- -Reboot machine after the deploy finishes. - -gui_on= ----------------- -This option is disabled by default, if enabled, it requires a command that -instructs kw to turn on the GUI. - -gui_off= ------------------ -This option is disabled by default, if enabled, it requires a command that -instructs kw to turn off the GUI. - EXAMPLES ======== -For these examples, we suppose the fields in your **kworkflow.config** file is -already configured. +For these examples, we assume that the relevant fields in your configuration +files (located by default in **.kw/**) have already been setup. We recommend +the use of ``kw config`` for managing your local and global configurations. First, if you are working in a specific kernel module, and if you want to install your recent changes in your local machine you can use:: diff --git a/documentation/tutorials/buildlinux.rst b/documentation/tutorials/buildlinux.rst index d2932b802..57dbc13ef 100644 --- a/documentation/tutorials/buildlinux.rst +++ b/documentation/tutorials/buildlinux.rst @@ -80,3 +80,12 @@ Well, that's it. kw will automatically infer the number of job slots for compiling based on the number of cores of your machine (i.e. when running make ``-j``, ** is an integer that specifies the number of processes that will run at the same time), and compilation will begin! + +Compiling patchsets +------------------- +You may want to try compiling every patch in your patchset to test if everything is alright. +You can do this by using the "from-sha" flag:: + + kw build --from-sha SHA + +This will compile every patch after the commit with given SHA to the branch HEAD. diff --git a/etc/kworkflow.config b/etc/kworkflow.config index 779e5b395..f1437d5d2 100644 --- a/etc/kworkflow.config +++ b/etc/kworkflow.config @@ -26,5 +26,8 @@ disable_statistics_data_track=no # Set a specific command to activate the GUI #gui_on=systemctl isolate graphical.target +#gui_on_after_reboot=systemctl set-default graphical.target + # Set a specific command to deactivate the GUI #gui_off=systemctl isolate multi-user.target +#gui_off_after_reboot=systemctl set-default multi-user.target \ No newline at end of file diff --git a/kw b/kw index 41b295151..aac10cc4e 100755 --- a/kw +++ b/kw @@ -321,7 +321,7 @@ function kw() complain 'Invalid option' kworkflow_help - return 1 + return 1 # EPERM ) ;; esac diff --git a/run_tests.sh b/run_tests.sh index 24de362a4..ffde2af78 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -64,7 +64,7 @@ function report_results() done fi - return 1 + return 1 # EPERM fi } diff --git a/setup.sh b/setup.sh index 34938b513..16561fdca 100755 --- a/setup.sh +++ b/setup.sh @@ -51,39 +51,91 @@ declare -r CONFIGS_PATH='configs' declare -r DOCS_VIRTUAL_ENV='docs_virtual_env' -function check_dependencies() +# This function identifies the missing packages required by the distribution. +# +# @distro: The distribution name (arch, debian, fedora, etc.) +# @deps_file: The path to the dependencies file for the distribution. +# +# Returns: +# Outputs a space-separated string with the missing packages. +function get_missing_packages() { - local package_list='' - local cmd='' - local distro - local ret - - distro=$(detect_distro '/') + local distro="$1" + local deps_file="$2" + local package_list + local installed if [[ "$distro" =~ 'arch' ]]; then while IFS='' read -r package; do - installed=$(pacman -Ql "$package" &> /dev/null) - [[ "$?" != 0 ]] && package_list="$package $package_list" - done < "$DOCUMENTATION/dependencies/arch.dependencies" - cmd="pacman -Sy --noconfirm ${package_list}" + pacman -Ql "$package" &> /dev/null + [[ "$?" != 0 ]] && package_list="${package} ${package_list}" + done < "$deps_file" elif [[ "$distro" =~ 'debian' ]]; then while IFS='' read -r package; do installed=$(dpkg-query -W --showformat='${Status}\n' "$package" 2> /dev/null | grep -c 'ok installed') - [[ "$installed" -eq 0 ]] && package_list="$package $package_list" - done < "$DOCUMENTATION/dependencies/debian.dependencies" - cmd="apt install -y $package_list" + [[ "$installed" -eq 0 ]] && package_list="${package} ${package_list}" + done < "$deps_file" elif [[ "$distro" =~ 'fedora' ]]; then while IFS='' read -r package; do - installed=$(rpm -q "$package" &> /dev/null) - [[ "$?" -ne 0 ]] && package_list="$package $package_list" - done < "$DOCUMENTATION/dependencies/fedora.dependencies" - cmd="dnf install -y $package_list" + rpm -q "$package" &> /dev/null + [[ "$?" -ne 0 ]] && package_list="${package} ${package_list}" + done < "$deps_file" + fi + + printf '%s\n' "$package_list" +} + +# This function installs the given packages. +# +# @cmd: The installation command with the packages to be installed. +# +# Returns: +# The return status of the installation command. +function install_packages() +{ + local cmd="$1" + + if [[ "$EUID" -eq 0 ]]; then + eval "$cmd" else - warning 'Unfortunately, we do not have official support for your distro (yet)' - warning 'Please, try to find the following packages:' - warning "$(cat "$DOCUMENTATION/dependencies/arch.dependencies")" - return 0 + eval "sudo ${cmd}" fi +} + +# This function checks and installs the necessary dependencies for the current +# distribution. +# +# Returns: +# Returns 0 if all dependencies are already installed or installation is +# successful. +function check_dependencies() +{ + local package_list + local cmd + local distro + local ret + + distro=$(detect_distro '/') + + package_list=$(get_missing_packages "$distro" "${DOCUMENTATION}/dependencies/${distro}.dependencies") + + case "$distro" in + arch*) + cmd="pacman -Sy --noconfirm ${package_list}" + ;; + debian*) + cmd="apt install -y ${package_list}" + ;; + fedora*) + cmd="dnf install -y ${package_list}" + ;; + *) + warning 'Unfortunately, we do not have official support for your distro (yet)' + warning 'Please, try to find the following packages:' + warning "$(cat "${DOCUMENTATION}/dependencies/arch.dependencies")" + return 0 + ;; + esac if [[ -n "$package_list" ]]; then if [[ "$FORCE" == 0 ]]; then @@ -93,12 +145,7 @@ function check_dependencies() fi fi - # Install system packages - if [[ "$EUID" -eq 0 ]]; then - eval "$cmd" - else - eval "sudo $cmd" - fi + install_packages "$cmd" ret="$?" # Installation failed... @@ -106,10 +153,90 @@ function check_dependencies() complain '[ERROR] Dependencies installation has failed. Aborting kw installation...' exit "$ret" fi + fi +} + +# This function checks and installs kernel build dependencies, i.e., +# dependencies that are necessary to build a Linux kernel. It supports three +# Linux distributions: Debian, Arch Linux, and Fedora. +# +# @distro: Detect the current distribution. +# +# Returns: +# Returns 0 if all necessary dependencies are already installed or installation +# is successful. and returns 22 (EINVAL), in case `@distro` is an unsupported +# OS type. The function can also exit the execution with 125 (ECANCELED) in case +# the user opts to abort the installation. +function check_and_install_kernel_build_dependencies() +{ + local distro="$1" + local kernel_build_deps_file + local package_list + local cmd + + kernel_build_deps_file="${DOCUMENTATION}/dependencies/kernel_build/${distro}.dependencies" + + package_list=$(get_missing_packages "$distro" "$kernel_build_deps_file") + + case "$distro" in + arch*) + cmd="pacman -Sy --noconfirm ${package_list}" + ;; + debian*) + cmd="apt install -y ${package_list}" + ;; + fedora*) + cmd="dnf install -y ${package_list}" + ;; + *) + complain "Unsupported OS type: ${distro}" + return 22 # EINVAL + ;; + esac + + if [[ -z "$package_list" ]]; then + warning 'All necessary kernel build dependencies are already installed.' + return 0 + fi + + if [[ -n "$package_list" ]]; then + if [[ "$FORCE" == 0 ]]; then + if [[ $(ask_yN "The following packages are required to build the kernel: ${package_list}"$'\nMay we install them?') =~ '0' ]]; then + complain 'Aborting installation. Kernel build dependencies not installed.' + exit 125 # ECANCELED + fi + fi + + install_packages "$cmd" + # Installation failed... + if [[ "$?" -ne 0 ]]; then + complain '[ERROR] Dependencies installation to build the kernel has failed. Aborting installation...' + exit "$?" + fi fi } +# This function initiates the process to ensure kernel build dependencies are +# installed. +function install_kernel_dev_deps() +{ + local distro + local ret + + distro=$(detect_distro '/') + + # Check if distro is equal to 'none' + if [[ "$distro" == 'none' ]]; then + complain 'Support for this distro is not available yet.' + exit 22 # EINVAL + fi + + # Use check_and_install_kernel_build_dependencies() to handle dependency + # installation + check_and_install_kernel_build_dependencies "$distro" +} + function generate_documentation() { local ret @@ -222,16 +349,18 @@ function usage() say 'usage: ./setup.sh option' say '' say 'Where option may be one of the following:' - say '--help | -h Display this usage message' - say "--install | -i Install $app_name" - say "--uninstall | -u Uninstall $app_name" - say '--skip-checks | -C Skip checks (use this when packaging)' - say '--skip-docs | -D Skip creation of man pages (use this when installing)' - say '--verbose | -v Explain what is being done' - say '--force | -f Never prompt' - say "--completely-remove | -r Remove $app_name and all files under its responsibility" - say "--docs | -d Build $app_name's documentation as HTML pages into ./build" - say "--enable-tracing | -t Install ${app_name} with tracing enabled (use it with --install)" + say '--help | -h Display this usage message' + say "--install | -i Install ${app_name}" + say '--install-kernel-dev-deps | -k Installs all necessary dependencies to build the kernel according to your distribution' + say "--full-installation | -F Install ${app_name} and its kernel development dependencies" + say "--uninstall | -u Uninstall ${app_name}" + say '--skip-checks | -C Skip checks (use this when packaging)' + say '--skip-docs | -D Skip creation of man pages (use this when installing)' + say '--verbose | -v Explain what is being done' + say '--force | -f Never prompt' + say "--completely-remove | -r Remove ${app_name} and all files under its responsibility" + say "--docs | -d Build ${app_name}'s documentation as HTML pages into ./build" + say "--enable-tracing | -t Install ${app_name} with tracing enabled (use it with --install)" } function confirm_complete_removal() @@ -516,6 +645,14 @@ function install_home() warning '-> For a better experience with kw, please, open a new terminal.' } +function full_installation() +{ + say 'Starting kw installation...' + install_home + say 'Installing kernel dependencies for build...' + install_kernel_dev_deps +} + function setup_bashrc_to_show_current_kw_env() { local config_file_template="${etcdir}/kw_prompt_current_env_name.sh" @@ -544,7 +681,7 @@ function setup_global_config_file() fi else warning "setup could not find $config_file_template" - return 2 + return 2 # ENOENT fi } @@ -604,6 +741,12 @@ case "$1" in --docs | -d) generate_documentation ;; + --install-kernel-dev-deps | -k) + install_kernel_dev_deps + ;; + --full-installation | -F) + full_installation + ;; *) complain 'Invalid number of arguments' usage diff --git a/src/_kw b/src/_kw index e5079cedb..71b318fc5 100644 --- a/src/_kw +++ b/src/_kw @@ -92,18 +92,18 @@ _kw_build() { _arguments : \ '(--verbose)--verbose[enable verbose mode]' \ - '(-i --info -c --clean -f --full-cleanup -n --menu -d --doc --ccache -w --warnings -S --cpu-scaling -s --save-log-to --llvm --cflags)'{-i,--info}'[display kernel release name, version and number of modules to compile]' \ - '(-c --clean -f --full-cleanup -i --info -n --menu -d --doc --ccache -w --warnings -S --cpu-scaling -s --save-log-to --llvm --cflags)'{-c,--clean}'[remove files generated by the kernel build system]' \ - '(-f --full-cleanup -c --clean -i --info -n --menu -d --doc --ccache -w --warnings -S --cpu-scaling -s --save-log-to --llvm --cflags)'{-f,--full-cleanup}'[reset the kernel tree to its default option integrated into env]' \ - '(-n --menu -i --info -c --clean -f --full-cleanup -d --doc --cflags)'{-n,--menu}'[invoke kernel menuconfig]' \ - '(-d --doc -i --info -c --clean -f --full-cleanup -n --menu --cflags)'{-d,--doc}'[build the kernel-doc]' \ - '(-i --info -c --clean -f --full-cleanup --cflags)--ccache[enable ccache during compilation tasks]' \ - '(-w --warnings -i --info -c --clean -f --full-cleanup --cflags)'{-w,--warnings}'[enable compilation warnings]:log level:(1 2 3 12 13 23 123)' \ - '(-S --cpu-scaling -i --info -c --clean -f --full-cleanup --cflags)'{-S,--cpu-scaling}'[set CPU usage]:scaling percentage: ' \ - '(-s --save-log-to -i --info -c --clean -f --full-cleanup --cflags)'{-s,--save-log-to}'[save full compilation log with the enabled warnings to the specified path]:log path:_files' \ - '(-i --info -c --clean -f --full-cleanup --cflags)--llvm[enable the usage of the LLVM toolchain]' \ - '(-i --info -c --clean -f --full-cleanup -n --menu -d --doc --ccache -w --warnings -S --cpu-scaling -s --save-log-to --llvm --cflags)--cflags[customize kernel compilation with specific flags]' - + '(-i --info -c --clean -f --full-cleanup -n --menu -d --doc --ccache -w --warnings -S --cpu-scaling -s --save-log-to --llvm --cflags --from-sha)'{-i,--info}'[display kernel release name, version and number of modules to compile]' \ + '(-c --clean -f --full-cleanup -i --info -n --menu -d --doc --ccache -w --warnings -S --cpu-scaling -s --save-log-to --llvm --cflags --from-sha)'{-c,--clean}'[remove files generated by the kernel build system]' \ + '(-f --full-cleanup -c --clean -i --info -n --menu -d --doc --ccache -w --warnings -S --cpu-scaling -s --save-log-to --llvm --cflags --from-sha)'{-f,--full-cleanup}'[reset the kernel tree to its default option integrated into env]' \ + '(-n --menu -i --info -c --clean -f --full-cleanup -d --doc --cflags --from-sha)'{-n,--menu}'[invoke kernel menuconfig]' \ + '(-d --doc -i --info -c --clean -f --full-cleanup -n --menu --cflags --from-sha)'{-d,--doc}'[build the kernel-doc]' \ + '(-i --info -c --clean -f --full-cleanup --cflags --from-sha)--ccache[enable ccache during compilation tasks]' \ + '(-w --warnings -i --info -c --clean -f --full-cleanup --cflags --from-sha)'{-w,--warnings}'[enable compilation warnings]:log level:(1 2 3 12 13 23 123)' \ + '(-S --cpu-scaling -i --info -c --clean -f --full-cleanup --cflags --from-sha)'{-S,--cpu-scaling}'[set CPU usage]:scaling percentage: ' \ + '(-s --save-log-to -i --info -c --clean -f --full-cleanup --cflags --from-sha)'{-s,--save-log-to}'[save full compilation log with the enabled warnings to the specified path]:log path:_files' \ + '(-i --info -c --clean -f --full-cleanup --cflags --from-sha)--llvm[enable the usage of the LLVM toolchain]' \ + '(-i --info -c --clean -f --full-cleanup -n --menu -d --doc --ccache -w --warnings -S --cpu-scaling -s --save-log-to --llvm --cflags --from-sha)--cflags[customize kernel compilation with specific flags]' \ + '(-i --info -c --clean -f --full-cleanup -n --menu -d --doc --ccache -w --warnings -S --cpu-scaling -s --save-log-to --llvm --cflags --from-sha)--from-sha[Compile all commits from given sha value to branch head]: :' } _kw_clear-cache() @@ -130,11 +130,11 @@ _kw_config() local configs='( vm.virtualizer vm.mount_point vm.qemu_hw_options vm.qemu_net_options vm.qemu_path_image kworkflow.ssh_user kworkflow.ssh_ip kworkflow.ssh_port kworkflow.ssh_configfile kworkflow.hostname - kworkflow.disable_statistics_data_track kworkflow.gui_on kworkflow.gui_off kworkflow.send_opts - kworkflow.blocked_emails kworkflow.checkpatch_opts kworkflow.get_maintainer_opts - notification.alert notification.sound_alert_command notification.visual_alert_command - build.arch build.kernel_img_name build.cross_compile build.menu_config build.doc_type build.cflags - build.cpu_scaling_factor build.enable_ccache build.warning_level build.use_llvm + kworkflow.disable_statistics_data_track kworkflow.gui_on kworkflow.gui_off kworkflow.gui_on_after_reboot + kworkflow.gui_off_after_reboot kworkflow.send_opts kworkflow.blocked_emails kworkflow.checkpatch_opts + kworkflow.get_maintainer_opts notification.alert notification.sound_alert_command + notification.visual_alert_command build.arch build.kernel_img_name build.cross_compile build.menu_config + build.doc_type build.cflags build.cpu_scaling_factor build.enable_ccache build.warning_level build.use_llvm deploy.kw_files_remote_path deploy.deploy_temporary_files_path deploy.deploy_default_compression deploy.dtb_copy_pattern deploy.default_deploy_target deploy.reboot_after_deploy deploy.strip_modules_debug_option mail.send_opts mail.blocked_emails mail.default_cc_recipients @@ -257,6 +257,8 @@ _kw_drm() '(--local)--remote[specify a remote as the target machine]: :' \ '(--gui-off)--gui-on[turn-on the target GUI]' \ '(--gui-on)--gui-off[turn-off the target GUI]' \ + '(--gui-off-after-reboot)--gui-on-after-reboot[turn-on the target GUI permanently]' \ + '(--gui-on-after-reboot)--gui-off-after-reboot[turn-off the target GUI permanently]' \ '--conn-available[show all connectors available in the target machine]' \ '--modes[show all available modes per card]' } @@ -282,6 +284,7 @@ _kw_explore() '(-a --all -l --log -g --grep)'{-a,--all}'[search for all matches in files under or not git management]' \ '(-c --only-source -H --only-header)'{-c,--only-source}'[show only results from the source]' \ '(-H --only-header -c --only-source)'{-H,--only-header}'[show only results from the header]' \ + '(-C --show-context)'{-C-,--show-context=-}'[set the context value]' \ '1: : ' \ '2: :_files' } diff --git a/src/bash_autocomplete.sh b/src/bash_autocomplete.sh index 4fb435e81..b6813ddea 100644 --- a/src/bash_autocomplete.sh +++ b/src/bash_autocomplete.sh @@ -21,7 +21,7 @@ function _kw_autocomplete() kw_options['init']='--arch --remote --target --force --template --verbose' kw_options['build']='--help --info --menu --cpu-scaling --ccache --llvm --clean - --full-cleanup --verbose --doc --warnings --save-log-to --cflags' + --full-cleanup --verbose --doc --warnings --save-log-to --cflags --from-sha' kw_options['b']="${kw_options['build']}" @@ -53,7 +53,7 @@ function _kw_autocomplete() kw_options['remote']='--add --remove --rename --list --global --set-default --verbose' - kw_options['explore']='--log --grep --all --only-header --only-source --exactly --verbose' + kw_options['explore']='--log --grep --all --only-header --only-source --exactly --show-context --verbose' kw_options['e']="${kw_options['explore']}" kw_options['pomodoro']='--set-timer --check-timer --show-tags --tag --description --help --verbose' @@ -78,8 +78,8 @@ function _kw_autocomplete() kw_options['patch-hub']='--help' - kw_options['drm']='--remote --local --gui-on --gui-off --load-module - --unload-module --conn-available --modes --verbose --help' + kw_options['drm']='--remote --local --gui-on --gui-off --gui-on-after-reboot --gui-off-after-reboot + --load-module --unload-module --conn-available --modes --verbose --help' kw_options['vm']='--mount --umount --up --alert --help' diff --git a/src/build.sh b/src/build.sh index c8221f69b..99759e06f 100644 --- a/src/build.sh +++ b/src/build.sh @@ -33,6 +33,9 @@ function build_kernel_main() local clean local output_kbuild_flag='' local cflags + local from_sha_arg + local sha_base + local merge_base parse_build_options "$@" @@ -59,6 +62,7 @@ function build_kernel_main() clean=${options_values['CLEAN']} full_cleanup=${options_values['FULL_CLEANUP']} cflags=${options_values['CFLAGS']} + from_sha_arg=${options_values['FROM_SHA_ARG']} [[ -n "${options_values['VERBOSE']}" ]] && flag='VERBOSE' flag=${flag:-'SILENT'} @@ -124,6 +128,41 @@ function build_kernel_main() return "$?" fi + if [[ -n "$from_sha_arg" ]]; then + # Check if there is a rebase in process. + if [[ -d .git/rebase-merge ]]; then + warning 'ERROR: Abort the repository rebase before continuing with build from sha (use "git rebase --abort")!' + return 125 # ECANCELED + elif [[ -f .git/MERGE_HEAD ]]; then + warning 'ERROR: Abort the repository merge before continuing with build from sha (use "git rebase --abort")!' + return 125 # ECANCELED + elif [[ -f .git/BISECT_LOG ]]; then + warning 'ERROR: Stop the repository bisect before continuing with build from sha (use "git bisect reset")!' + return 125 # ECANCELED + elif [[ -d .git/rebase-apply ]]; then + printf 'ERROR: Abort the repository patch apply before continuing with build from sha (use "git am --abort")!' + return 125 # ECANCELED + fi + + # Check if given SHA represents real commit + cmd_manager 'SILENT' "git cat-file -e ${from_sha_arg}^{commit} 2> /dev/null" + if [[ "$?" != 0 ]]; then + complain "ERROR: The given SHA (${from_sha_arg}) does not represent a valid commit sha." + return 22 # EINVAL + fi + + # Check if given SHA is in working tree. + sha_base=$(git rev-parse --verify "$from_sha_arg") + merge_base=$(git merge-base "$from_sha_arg" HEAD) + if [[ "$sha_base" != "$merge_base" ]]; then + complain "ERROR: Given SHA (${from_sha_arg}) is invalid. Check if it is an ancestor of the branch head." + return 22 # EINVAL + fi + + build_from_sha "$flag" "$from_sha_arg" + return "$?" + fi + command="make ${optimizations} ${llvm}ARCH=${platform_ops}${warnings}" if [[ -n "$cflags" ]]; then @@ -247,9 +286,33 @@ function full_cleanup() cmd_manager "$flag" "$cmd" } +# This functions uses iteractive 'git rebase' with '--exec' flag under the hood +# to apply a 'kw build' over each commit from SHA to branch head. +# +# @flag How to display a command, see `src/lib/kwlib.sh` function `cmd_manager`. +# @sha The SHA from the first commit to be compiled until the branch head. +# +# Return: +# 0 if successfully compiled patchset, 125 (ECANCELED) otherwise. +function build_from_sha() +{ + local flag="$1" + local sha="$2" + local cmd + + flag=${flag:-'SILENT'} + cmd="git rebase ${sha} --exec 'kw build'" + cmd_manager "$flag" "$cmd" + + if [[ "$?" != 0 ]]; then + complain "kw build failed during the compilation of a patch! Check the rebase in progress for more information." + return 125 #ECANCELED + fi +} + function parse_build_options() { - local long_options='help,info,menu,doc,ccache,cpu-scaling:,warnings::,save-log-to:,llvm,clean,full-cleanup,verbose,cflags:' + local long_options='help,info,menu,doc,ccache,cpu-scaling:,warnings::,save-log-to:,llvm,clean,full-cleanup,verbose,cflags:,from-sha:' local short_options='h,i,n,d,S:,w::,s:,c,f' local doc_type local file_name_size @@ -279,6 +342,7 @@ function parse_build_options() options_values['FULL_CLEANUP']='' options_values['VERBOSE']='' options_values['CFLAGS']="${build_config[cflags]}" + options_values['FROM_SHA_ARG']='' # Check llvm option if [[ ${options_values['USE_LLVM_TOOLCHAIN']} =~ 'yes' ]]; then @@ -362,6 +426,10 @@ function parse_build_options() options_values['LOG_PATH']="$2" shift 2 ;; + --from-sha) + options_values['FROM_SHA_ARG']="$2" + shift 2 + ;; --) shift ;; @@ -394,7 +462,8 @@ function build_help() ' build (-c | --clean) - Clean option integrated into env' \ ' build (-f | --full-cleanup) - Reset the kernel tree to its default option integrated into env' \ ' build (--cflags) - Customize kernel compilation with specific flags' \ - ' build (--verbose) - Show a detailed output' + ' build (--verbose) - Show a detailed output' \ + ' build (--from-sha ) - Build all commits from to actual commit' } # Every time build.sh is loaded its proper configuration has to be loaded as well diff --git a/src/debug.sh b/src/debug.sh index 019d2fe5d..7a8a317ca 100644 --- a/src/debug.sh +++ b/src/debug.sh @@ -48,7 +48,7 @@ function debug_main() if [[ "$?" -gt 0 ]]; then complain "Invalid option: ${options_values['ERROR']}" debug_help - return 22 + return 22 # EINVAL fi test_mode="${options_values['TEST_MODE']}" @@ -208,7 +208,7 @@ function reset_debug() # TODO: We should check if the VM is up and running fi - cmd_remotely "$reset_cmd" "$flag" "$remote" "$port" "$user" + cmd_remotely "$flag" "$reset_cmd" "$remote" "$port" "$user" ;; esac } @@ -275,7 +275,7 @@ function dmesg_debug() # TODO: We should check if the VM is up and running fi - cmd_remotely "$cmd" "$flag" '' '' '' '' "$save_following_log" + cmd_remotely "$flag" "$cmd" '' '' '' '' "$save_following_log" ;; esac } @@ -387,13 +387,13 @@ function event_debug() if [[ -n "$list" ]]; then show_verbose "$flag" "$command" - list_output=$(cmd_remotely "$command" 'SILENT' "$remote" "$port" "$user") + list_output=$(cmd_remotely 'SILENT' "$command" "$remote" "$port" "$user") show_list "$list_output" "$event" ret="$?" return "$ret" fi - cmd_remotely "$command" "$flag" "$remote" "$port" "$user" '' "$save_following_log" + cmd_remotely "$flag" "$command" "$remote" "$port" "$user" '' "$save_following_log" # If we used --cmd, we need to retrieve the log if [[ -n "$user_cmd" ]]; then @@ -442,7 +442,7 @@ function ftrace_list() fi show_verbose "$flag" "$cmd_list" - raw_data=$(cmd_remotely "$cmd_list" 'SILENT' "$remote" "$port" "$user") + raw_data=$(cmd_remotely 'SILENT' "$cmd_list" "$remote" "$port" "$user") ret="$?" ;; esac @@ -531,7 +531,7 @@ function ftrace_debug() # TODO: We should check if the VM is up and running fi - cmd_remotely "$cmd_ftrace" "$flag" '' '' '' '' "$save_following_log" + cmd_remotely "$flag" "$cmd_ftrace" '' '' '' '' "$save_following_log" ret="$?" # If we used --cmd, we need to retrieve the log @@ -900,7 +900,7 @@ function stop_debug() port="${remote_parameters['REMOTE_PORT']}" user="${remote_parameters['REMOTE_USER']}" - cmd_remotely "$stop_dmesg_cmd" "$flag" "$remote" "$port" "$user" + cmd_remotely "$flag" "$stop_dmesg_cmd" "$remote" "$port" "$user" ;; esac fi @@ -961,7 +961,7 @@ function parser_debug_options() populate_remote_info "$2" if [[ "$?" == 22 ]]; then options_values['ERROR']="$option" - return 22 + return 22 # EINVAL fi options_values['TARGET']="$REMOTE_TARGET" shift 2 @@ -1029,7 +1029,7 @@ function parser_debug_options() ;; *) options_values['ERROR']="$1" - return 22 + return 22 # EINVAL ;; esac done diff --git a/src/deploy.sh b/src/deploy.sh index 98d3407c5..50c661d95 100644 --- a/src/deploy.sh +++ b/src/deploy.sh @@ -352,7 +352,7 @@ function prepare_distro_for_deploy() local cmd="$REMOTE_INTERACE_CMD_PREFIX" cmd+=" --deploy-setup ${flag} ${target}" - cmd_remotely "$cmd" "$flag" + cmd_remotely "$flag" "$cmd" ;; esac } @@ -389,7 +389,7 @@ function update_status_log() ;; 3) # REMOTE_TARGET cmd="${metadata_string} >> ${kw_status_path}" - cmd_remotely "$cmd" "$flag" + cmd_remotely "$flag" "$cmd" ;; esac } @@ -420,7 +420,7 @@ function check_setup_status() ret="$?" ;; 3) # REMOTE_TARGET - cmd_remotely "$cmd" "$flag" + cmd_remotely "$flag" "$cmd" ret="$?" ;; esac @@ -563,7 +563,7 @@ function prepare_remote_dir() '--archive' "$remote" "$port" "$user" 'quiet' else say '* Sending kw to the remote' - cmd_remotely "mkdir -p $REMOTE_KW_DEPLOY" "$flag" + cmd_remotely "$flag" "mkdir -p $REMOTE_KW_DEPLOY" if [[ -n ${remote_parameters['REMOTE_FILE']} && -n ${remote_parameters['REMOTE_FILE_HOST']} ]]; then cmd="scp -q -F ${remote_parameters['REMOTE_FILE']} $files_to_send ${remote_parameters['REMOTE_FILE_HOST']}:$REMOTE_KW_DEPLOY" @@ -575,9 +575,9 @@ function prepare_remote_dir() fi # Removes temporary directory if already existent - cmd_remotely "rm --preserve-root=all --recursive --force -- ${KW_DEPLOY_TMP_FILE}" "$flag" + cmd_remotely "$flag" "rm --preserve-root=all --recursive --force -- ${KW_DEPLOY_TMP_FILE}" # Create temporary folder - cmd_remotely "mkdir -p $KW_DEPLOY_TMP_FILE" "$flag" + cmd_remotely "$flag" "mkdir -p $KW_DEPLOY_TMP_FILE" } # Create the temporary folder for local deploy. @@ -663,7 +663,7 @@ function run_list_installed_kernels() local cmd="$REMOTE_INTERACE_CMD_PREFIX" cmd+=" --list-kernels $flag $single_line $all" - cmd_remotely "$cmd" "$flag" + cmd_remotely "$flag" "$cmd" ;; esac @@ -707,7 +707,7 @@ function collect_target_info_for_deploy() local cmd="$REMOTE_INTERACE_CMD_PREFIX" cmd+=" --collect-info $flag $target" - data=$(cmd_remotely "$cmd" "$flag") + data=$(cmd_remotely "$flag" "$cmd") ;; esac @@ -780,7 +780,7 @@ function run_kernel_uninstall() # line break with `\`; this may allow us to break a huge line like this. local cmd="$REMOTE_INTERACE_CMD_PREFIX" cmd+=" --uninstall-kernels '$reboot' 'remote' '$kernels_target_list' '$flag' '$force'" - cmd_remotely "$cmd" "$flag" + cmd_remotely "$flag" "$cmd" ;; esac } @@ -853,7 +853,7 @@ function modules_install() # Execute script cmd="$REMOTE_INTERACE_CMD_PREFIX --modules ${release}.kw.tar" - cmd_remotely "$cmd" "$flag" + cmd_remotely "$flag" "$cmd" ;; esac } @@ -1338,7 +1338,7 @@ function run_kernel_install() cmd="$REMOTE_INTERACE_CMD_PREFIX" cmd+=" --kernel-update $cmd_parameters" - cmd_remotely "$cmd" "$flag" "$remote" "$port" + cmd_remotely "$flag" "$cmd" "$remote" "$port" human_install_kernel_message "$?" return "$?" ;; diff --git a/src/device_info.sh b/src/device_info.sh index 4beb7781b..fb1551305 100644 --- a/src/device_info.sh +++ b/src/device_info.sh @@ -88,7 +88,7 @@ function get_ram() ;; 3) # REMOTE_TARGET show_verbose "$flag" "$cmd" - ram=$(cmd_remotely "$cmd" 'SILENT') + ram=$(cmd_remotely 'SILENT' "$cmd") ;; esac @@ -131,10 +131,10 @@ function get_cpu() ;; 3) # REMOTE_TARGET show_verbose "$flag" "$cmd_model" - cpu_model=$(cmd_remotely "$cmd_model" 'SILENT') + cpu_model=$(cmd_remotely 'SILENT' "$cmd_model") show_verbose "$flag" "$cmd_frequency" - cpu_frequency=$(cmd_remotely "$cmd_frequency" 'SILENT') + cpu_frequency=$(cmd_remotely 'SILENT' "$cmd_frequency") ;; esac @@ -193,7 +193,7 @@ function get_disk() ;; 3) # REMOTE_TARGET show_verbose "$flag" "$cmd" - info=$(cmd_remotely "$cmd" 'SILENT') + info=$(cmd_remotely 'SILENT' "$cmd") ;; esac @@ -254,7 +254,7 @@ function get_os() root_path='/' cmd="cat $(join_path "$root_path" "$os_release_path")" show_verbose "$flag" "$cmd" - raw_os_release=$(cmd_remotely "$cmd" 'SILENT') + raw_os_release=$(cmd_remotely 'SILENT' "$cmd") ;; esac @@ -313,7 +313,7 @@ function get_desktop_environment() ;; 3) # REMOTE_TARGET show_verbose "$flag" "$cmd" - desktop_env=$(cmd_remotely "$cmd" 'SILENT') + desktop_env=$(cmd_remotely 'SILENT' "$cmd") ;; esac @@ -379,11 +379,11 @@ function get_gpu() ;; 3) # REMOTE_TARGET show_verbose "$flag" "$cmd_pci_address" - pci_addresses=$(cmd_remotely "$cmd_pci_address" 'SILENT') + pci_addresses=$(cmd_remotely 'SILENT' "$cmd_pci_address") for g in $pci_addresses; do cmd="lspci -v -s ${g}" show_verbose "$flag" "$cmd" - gpu_info=$(cmd_remotely "$cmd" 'SILENT') + gpu_info=$(cmd_remotely 'SILENT' "$cmd") cmd="printf '%s\n' '${gpu_info}' | sed --quiet --regexp-extended '/Subsystem/s/\s*.*:\s+(.*)/\1/p'" show_verbose "$flag" "$cmd" @@ -444,20 +444,20 @@ function get_motherboard() ;; 3) # REMOTE_TARGET show_verbose "$flag" "$cmd_name" - mb_name=$(cmd_remotely "$cmd_name" 'SILENT') + mb_name=$(cmd_remotely 'SILENT' "$cmd_name") show_verbose "$flag" "$cmd_vendor" - mb_vendor=$(cmd_remotely "$cmd_vendor" 'SILENT') + mb_vendor=$(cmd_remotely 'SILENT' "$cmd_vendor") # Fallback if [[ -z "$mb_name" ]]; then show_verbose "$flag" "$fallback_name_cmd" - mb_name=$(cmd_remotely "$fallback_name_cmd" 'SILENT') + mb_name=$(cmd_remotely 'SILENT' "$fallback_name_cmd") fi if [[ -z "$mb_vendor" ]]; then show_verbose "$flag" "$fallback_vendor_cmd" - mb_vendor=$(cmd_remotely "$fallback_vendor_cmd" 'SILENT') + mb_vendor=$(cmd_remotely 'SILENT' "$fallback_vendor_cmd") fi ;; esac @@ -508,10 +508,10 @@ function get_chassis() 3) # REMOTE_TARGET cmd="test -f ${dmi_file_path}" show_verbose "$flag" "$cmd" - cmd_remotely "$cmd" "$flag" + cmd_remotely "$flag" "$cmd" if [[ "$?" == 0 ]]; then show_verbose "$flag" "$dmi_cmd" - chassis_type=$(cmd_remotely "$dmi_cmd" 'SILENT') + chassis_type=$(cmd_remotely 'SILENT' "$dmi_cmd") fi ;; esac diff --git a/src/explore.sh b/src/explore.sh index c1f88f604..c69c2d751 100644 --- a/src/explore.sh +++ b/src/explore.sh @@ -15,6 +15,7 @@ function explore_main() local flag local search local path + local context local ret if [[ "$1" =~ -h|--help ]]; then @@ -31,6 +32,7 @@ function explore_main() flag="${options_values['TEST_MODE']:-'SILENT'}" search="${options_values['SEARCH']}" path="${options_values['PATH']:-'.'}" + context="${options_values['CONTEXT']:-0}" [[ -n "${options_values['VERBOSE']}" ]] && flag='VERBOSE' @@ -48,19 +50,19 @@ function explore_main() if [[ "${options_values['TYPE']}" -eq 2 ]]; then # Use GNU GREP - explore_files_gnu_grep "$search" "$path" "$flag" + explore_files_gnu_grep "$search" "$path" "$context" "$flag" return fi if [[ "${options_values['TYPE']}" -eq 3 ]]; then # Search in directories controlled or not by git - explore_all_files_git "$search" "$path" "$flag" + explore_all_files_git "$search" "$path" "$context" "$flag" return fi if [[ -z "${options_values['TYPE']}" ]]; then # Search in files under git control - explore_files_under_git "$search" "$path" "$flag" + explore_files_under_git "$search" "$path" "$context" "$flag" return fi } @@ -75,8 +77,8 @@ function explore_main() # This function also set options_values function parse_explore_options() { - local long_options='log,grep,all,only-header,only-source,exactly,verbose' - local short_options='l,g,a,H,c' + local long_options='log,grep,all,only-header,only-source,exactly,verbose,show-context::' + local short_options='l,g,a,H,c,C::' local options if [[ "$#" -eq 0 ]]; then @@ -97,6 +99,7 @@ function parse_explore_options() options_values['TYPE']='' options_values['SCOPE']='' options_values['EXACTLY']='' + options_values['CONTEXT']='' options_values['VERBOSE']='' eval "set -- $options" @@ -158,6 +161,24 @@ function parse_explore_options() ;; --verbose) options_values['VERBOSE']=1 + shift + ;; + --show-context | -C) + if [[ -n "${options_values['CONTEXT']}" ]]; then + options_values['ERROR']='Invalid arguments: Multiple context values!' + return 22 # EINVAL + fi + + if [[ ! "$2" ]]; then + options_values['CONTEXT']=3 + elif [[ ! "$2" =~ ^[0-9]+$ ]]; then + options_values['ERROR']='Context value must be a non-negative integer!' + return 22 # EINVAL + else + options_values['CONTEXT']="$2" + shift + fi + shift ;; --) # End of options, beginning of arguments @@ -196,7 +217,7 @@ function explore_git_log() flag=${flag:-'SILENT'} - cmd_manager "$flag" "git log --grep='$search_string' $path" + cmd_manager "$flag" "git log --grep='${search_string}' ${path}" } # This function searches string in files under git control. @@ -209,11 +230,12 @@ function explore_files_under_git() { local regex="$1" local path="$2" - local flag="$3" + local context="$3" + local flag="$4" flag=${flag:-'SILENT'} - cmd_manager "$flag" "git grep -e '$regex' -nI $path" + cmd_manager "$flag" "git grep --context ${context} -e '${regex}' --line-number -I ${path}" } # This function uses git grep tool to search string in files under or not git @@ -228,11 +250,12 @@ function explore_all_files_git() { local regex="$1" local path="$2" - local flag="$3" + local context="$3" + local flag="$4" flag=${flag:-'SILENT'} - cmd_manager "$flag" "git grep --no-index -e '$regex' -nI $path" + cmd_manager "$flag" "git grep --no-index --context ${context} -e '${regex}' --line-number -I ${path}" } # This function allows the use of gnu grep utility to manages the search for @@ -246,11 +269,12 @@ function explore_files_gnu_grep() { local regex="$1" local path="$2" - local flag="$3" + local context="$3" + local flag="$4" flag=${flag:-'SILENT'} - cmd_manager "$flag" "grep --color -nrI $path -e '$regex'" + cmd_manager "$flag" "grep --color --line-number --recursive -I ${path} --context ${context} -e '${regex}'" } function explore_help() @@ -267,5 +291,6 @@ function explore_help() ' explore,e --all,-a - Search for all match under or not of git management' \ ' explore,e --only-source,-c - Search for all in source files' \ ' explore,e --only-header,-H - Search for all in header files' \ + ' explore,e --show-context[=],-C[] - Print lines of output context (default: 3)' \ ' explore,e --verbose - Show a detailed output' } diff --git a/src/kernel_config_manager.sh b/src/kernel_config_manager.sh index ffd4a214c..9935fdbe3 100644 --- a/src/kernel_config_manager.sh +++ b/src/kernel_config_manager.sh @@ -245,7 +245,7 @@ function get_config_from_proc() case "$target" in 1) # VM # We do not support this option with VM - return 95 + return 95 # ENOTSUP ;; 2) # LOCAL # Try to find /proc/config, if we cannot find, attempt to load the module @@ -259,13 +259,13 @@ function get_config_from_proc() return 0 ;; 3) # REMOTE - cmd_remotely "[ -f ${PROC_CONFIG_PATH} ]" "$flag" + cmd_remotely "$flag" "[ -f ${PROC_CONFIG_PATH} ]" if [[ "$?" != 0 ]]; then - cmd_remotely "$CMD_LOAD_CONFIG_MODULE" "$flag" + cmd_remotely "$flag" "$CMD_LOAD_CONFIG_MODULE" [[ "$?" != 0 ]] && return 95 # Operation not supported fi - cmd_remotely "$CMD_GET_CONFIG" "$flag" + cmd_remotely "$flag" "$CMD_GET_CONFIG" [[ "$?" != 0 ]] && return 95 # Operation not supported remote2host "$flag" "/tmp/${output}" "$config_base_path" return 0 @@ -313,8 +313,8 @@ function get_config_from_boot() return 0 ;; 3) # REMOTE - kernel_release=$(cmd_remotely 'uname -r' "$flag") - cmd_remotely "[ -f ${root}boot/config-${kernel_release} ]" "$flag" + kernel_release=$(cmd_remotely "$flag" 'uname -r') + cmd_remotely "$flag" "[ -f ${root}boot/config-${kernel_release} ]" [[ "$?" != 0 ]] && return 95 # ENOTSUP remote2host "$flag" "${root}boot/config-${kernel_release}" "$config_base_path" @@ -476,7 +476,7 @@ function fetch_config() mods=$(cmd_manager "$flag" 'lsmod') ;; 3) # REMOTE - mods=$(cmd_remotely 'lsmod' "$flag") + mods=$(cmd_remotely "$flag" 'lsmod') ;; esac diff --git a/src/kw_remote.sh b/src/kw_remote.sh index 5f6a88dd8..98a98ffb1 100644 --- a/src/kw_remote.sh +++ b/src/kw_remote.sh @@ -406,7 +406,7 @@ function parse_remote_options() return 22 # EINVAL elif [[ "${options_values['DEFAULT_REMOTE']}" == 1 ]]; then options_values['ERROR']='Expected a string values after --set-default=' - return 22 + return 22 # EINVAL elif [[ -z "${options_values['ADD']}" && -z "${options_values['REMOVE']}" && -z "${options_values['RENAME']}" && -z "${options_values['LIST']}" && -z "${options_values['DEFAULT_REMOTE']}" ]]; then diff --git a/src/kw_ssh.sh b/src/kw_ssh.sh index 5d0591fb1..f446ba2fe 100644 --- a/src/kw_ssh.sh +++ b/src/kw_ssh.sh @@ -316,7 +316,7 @@ function parser_ssh_options() populate_remote_info "$2" if [[ "$?" == 22 ]]; then options_values['ERROR']="$option" - return 22 + return 22 # EINVAL fi shift 2 ;; @@ -357,7 +357,7 @@ function parser_ssh_options() ;; *) options_values['ERROR']="$1" - return 22 + return 22 # EINVAL ;; esac done diff --git a/src/lib/dialog_ui.sh b/src/lib/dialog_ui.sh index 5c54cc2e3..9932c0688 100644 --- a/src/lib/dialog_ui.sh +++ b/src/lib/dialog_ui.sh @@ -931,7 +931,7 @@ function prettify_string() local variable_to_concatenate="$2" if [[ -z "$fixed_text" || -z "$variable_to_concatenate" ]]; then - return 22 + return 22 # EINVAL fi printf '\Zb\Z6%s\Zn%s\\n' "$fixed_text" "$variable_to_concatenate" diff --git a/src/lib/kw_string.sh b/src/lib/kw_string.sh index 73d406a01..853f29ed0 100644 --- a/src/lib/kw_string.sh +++ b/src/lib/kw_string.sh @@ -29,7 +29,7 @@ function str_is_a_number() value=$(str_strip "$value") [[ "$value" =~ ^[-]?[0-9]+$ ]] && return 0 - return 1 + return 1 # EPERM } # Calculate the length of a string @@ -206,7 +206,7 @@ function str_has_special_characters() local str="$*" [[ "$str" == *['!'@#\$%^\&*\(\)+]* ]] && return 0 - return 1 + return 1 # EPERM } # Get value under double-quotes. This function only returns the first match if diff --git a/src/lib/kwlib.sh b/src/lib/kwlib.sh index bdebbe11c..f67ccc6d4 100644 --- a/src/lib/kwlib.sh +++ b/src/lib/kwlib.sh @@ -169,7 +169,7 @@ function is_kernel_root() -d "${DIR}/scripts" ]]; then return 0 fi - return 1 + return 1 # EPERM } # Finds the root of the linux kernel repo containing the given file @@ -268,7 +268,7 @@ function is_a_patch() local file_content if [[ ! -f "$FILE_PATH" ]]; then - return 1 + return 1 # EPERM fi file_content=$(< "$FILE_PATH") @@ -284,7 +284,7 @@ function is_a_patch() for expected_str in "${PATCH_EXPECTED_STRINGS[@]}"; do if [[ ! "$file_content" =~ $expected_str ]]; then - return 1 + return 1 # EPERM fi done diff --git a/src/lib/remote.sh b/src/lib/remote.sh index 83968dd32..af9d60c03 100644 --- a/src/lib/remote.sh +++ b/src/lib/remote.sh @@ -67,7 +67,7 @@ function is_ssh_connection_configured() ret="$?" # User canceled the manual update - [[ "$ret" == 125 ]] && return 125 + [[ "$ret" == 125 ]] && return 125 # ECANCELED # Some other unknown error occurred [[ "$ret" != 0 ]] && return 101 # ENETUNREACH @@ -246,8 +246,8 @@ function ssh_connection_failure_message() # If no command is specified, we finish the execution and return 22 function cmd_remotely() { - local command="$1" - local flag=${2:-'HIGHLIGHT_CMD'} + local flag=${1:-'HIGHLIGHT_CMD'} + local command="$2" local remote=${3:-${remote_parameters['REMOTE_IP']}} local port=${4:-${remote_parameters['REMOTE_PORT']}} local user=${5:-${remote_parameters['REMOTE_USER']}} @@ -371,7 +371,7 @@ function which_distro() local output cmd='cat /etc/os-release' - output=$(cmd_remotely "$cmd" "$flag" "$remote" "$port" "$user") + output=$(cmd_remotely "$flag" "$cmd" "$remote" "$port" "$user") # TODO: I think we can find a better way to test this... if [[ "$flag" =~ 'TEST_MODE' ]]; then printf '%s' "$output" diff --git a/src/lib/web.sh b/src/lib/web.sh index f10788520..bde41f70a 100644 --- a/src/lib/web.sh +++ b/src/lib/web.sh @@ -74,7 +74,7 @@ function is_html_file() grep --silent '\(\|\)' "$file_path" [[ "$?" == 0 ]] && return 0 - return 1 + return 1 # EPERM } # This function recieves a string and converts it to contain only characters that diff --git a/src/mail.sh b/src/mail.sh index 8f9d19aac..8680e8f83 100644 --- a/src/mail.sh +++ b/src/mail.sh @@ -600,7 +600,7 @@ function mail_verify() if [[ "${#missing_conf}" -gt 0 ]]; then complain 'Missing configurations required for send-email:' printf ' %s\n' "${missing_conf[@]}" - return 22 + return 22 # EINVAL fi success 'It looks like you are ready to send patches as:' diff --git a/src/maintainers.sh b/src/maintainers.sh index 319a5fd30..a7e745c76 100644 --- a/src/maintainers.sh +++ b/src/maintainers.sh @@ -59,7 +59,7 @@ function maintainers_main() # Check if is a valid path if [[ ! -d "$FILE_OR_DIR" && ! -f "$FILE_OR_DIR" ]]; then complain 'Invalid path' - return 1 + return 1 # EPERM fi FILE_OR_DIR="$(realpath "${FILE_OR_DIR}")" @@ -68,7 +68,7 @@ function maintainers_main() if ! is_a_patch "$FILE_OR_DIR"; then if [[ -n "$update_patch" ]]; then complain 'Option --update-patch was passed but given path is not a patch.' - return 1 + return 1 # EPERM fi is_file_a_patch=false script_options="${script_options} -f" @@ -85,7 +85,7 @@ function maintainers_main() # Check if kernel root was found. if [[ -z "$kernel_root" ]]; then complain 'Neither the given path nor the working path is in a kernel tree.' - return 1 + return 1 # EPERM fi # If file is not a patch and outside a kernel tree, it must be an user's @@ -93,7 +93,7 @@ function maintainers_main() # because it is most likely a user's mistake. So better let the user know. if ! "$is_file_a_patch" && ! "$is_file_inside_kernel_tree"; then complain 'The given file is not a patch and is outside a kernel tree.' - return 1 + return 1 # EPERM fi cmd_manager "$flag" "cd ${kernel_root}" @@ -160,9 +160,20 @@ function print_files_authors() local printed_authors_separator=false for file in "${files[@]}"; do - authors=$(grep --only-matching --extended-regexp 'MODULE_AUTHOR *\(.*\)' "$file" | - sed --regexp-extended 's/(MODULE_AUTHOR *\( *\"|\" *\))//g' | - sed --expression ':a' --expression 'N' --expression '$!ba' --expression 's/\n/, /g') + # Match everything inside the parenthesis of `MODULE_AUTHOR ()`, with arbitrary white spaces + # before `(`, while handling multi-line cases. The `\K` escape sequence resets the grep's match, + # that is, it excludes `MODULE_AUTHOR *\(` from the final result + authors=$(grep --null-data --perl-regexp --only-matching 'MODULE_AUTHOR\s*\(\K[^)]*' "$file" | tr --delete '\0') + + # Trim contents from first grep deleting `\n` and `\` with any adjacent white spaces, + # and substitute leading white spaces to a single one. + authors=$(sed --expression 's/\\n//g' --expression 's/\s*\\\s*//g' --expression 's/^\s*/ /g' <<< "$authors") + authors=$(tr --delete '\n' <<< "$authors") + + # Finally, delete string quotes by substituting them to commas to separate the authors, removing + # remaining blank characters at the end of the output. + authors=$(sed --expression 's/\"\s*\"/, /g' --expression 's/\s*\"//g' --expression 's/\s*$//g' <<< "$authors") + if [[ -n "$authors" ]]; then if [[ "$printed_authors_separator" = false ]]; then say "$SEPARATOR" diff --git a/src/plugins/kernel_install/utils.sh b/src/plugins/kernel_install/utils.sh index adcff66e9..c59e06254 100644 --- a/src/plugins/kernel_install/utils.sh +++ b/src/plugins/kernel_install/utils.sh @@ -40,7 +40,7 @@ function cmd_manager() ;; esac - eval "$@" + eval "$*" } function command_exists() @@ -197,7 +197,8 @@ function collect_deploy_info() bootloader="[bootloader]=$bootloader" # Get distro - distro=$(cat /etc/*-release | grep -w 'ID\(_LIKE\)\?' | cut -d = -f 2 | xargs echo) + distro=$(cat /etc/*-release | grep --word-regexp 'ID\(_LIKE\)\?' | cut --delimiter = --fields 2 | xargs printf '%s ') + distro="${distro::-1}" distro="[distro]='$distro'" # Build associative array data diff --git a/src/plugins/subsystems/drm/drm.sh b/src/plugins/subsystems/drm/drm.sh index bf4c9c9f1..91d820589 100644 --- a/src/plugins/subsystems/drm/drm.sh +++ b/src/plugins/subsystems/drm/drm.sh @@ -1,6 +1,6 @@ -. "${KW_LIB_DIR}/lib/kw_config_loader.sh" --source-only -. "${KW_LIB_DIR}/lib/remote.sh" --source-only -. "${KW_LIB_DIR}/lib/kwlib.sh" --source-only +include "${KW_LIB_DIR}/lib/kw_config_loader.sh" +include "${KW_LIB_DIR}/lib/remote.sh" +include "${KW_LIB_DIR}/lib/kwlib.sh" declare -gr UNLOAD='UNLOAD' declare -gA options_values @@ -12,6 +12,8 @@ function drm_main() local target local gui_on local gui_off + local gui_on_after_reboot + local gui_off_after_reboot local conn_available local remote local load_module @@ -26,14 +28,16 @@ function drm_main() parse_drm_options "$@" if [[ "$?" -gt 0 ]]; then - complain "Invalid option: ${options_values['ERROR']} $target $gui_on $gui_off ${remote_parameters['REMOTE_IP']} ${remote_parameters['REMOTE_PORT']}" + complain "Invalid option: ${options_values['ERROR']} ${target} ${gui_on} ${gui_off} ${remote_parameters['REMOTE_IP']} ${remote_parameters['REMOTE_PORT']}" drm_help - return 22 + return 22 # EINVAL fi target="${options_values['TARGET']}" gui_on="${options_values['GUI_ON']}" gui_off="${options_values['GUI_OFF']}" + gui_on_after_reboot="${options_values['GUI_ON_AFTER_REBOOT']}" + gui_off_after_reboot="${options_values['GUI_OFF_AFTER_REBOOT']}" conn_available="${options_values['CONN_AVAILABLE']}" modes_available="${options_values['MODES_AVAILABLE']}" help_opt="${options_values['HELP']}" @@ -56,7 +60,7 @@ function drm_main() if [[ -n "$load_module" ]]; then module_control 'LOAD' "$target" "$remote" "$load_module" "$flag" if [[ "$?" != 0 ]]; then - return 22 + return 22 # EINVAL fi fi @@ -64,6 +68,10 @@ function drm_main() gui_control 'ON' "$target" "$remote" "$flag" elif [[ "$gui_off" == 1 ]]; then gui_control 'OFF' "$target" "$remote" "$flag" + elif [[ "$gui_on_after_reboot" == 1 ]]; then + gui_control 'ON_AFTER_REBOOT' "$target" "$remote" "$flag" + elif [[ "$gui_off_after_reboot" == 1 ]]; then + gui_control 'OFF_AFTER_REBOOT' "$target" "$remote" "$flag" fi if [[ -n "$unload_module" ]]; then @@ -100,7 +108,7 @@ function module_control() local unformatted_remote="$3" local parameters="$4" local flag="$5" - local module_cmd="" + local module_cmd='' local remote local port @@ -109,18 +117,18 @@ function module_control() module_cmd=$(convert_module_info "$operation" "$parameters") if [[ "$?" != 0 ]]; then complain 'Wrong parameter in --[un]load-module=' - return 22 + return 22 # EINVAL fi case "$target" in 2) # LOCAL - cmd_manager "$flag" "sudo bash -c \"$module_cmd\"" + cmd_manager "$flag" "sudo bash -c \"${module_cmd}\"" ;; 3) # REMOTE remote=$(get_based_on_delimiter "$unformatted_remote" ':' 1) port=$(get_based_on_delimiter "$unformatted_remote" ':' 2) - cmd_remotely "$module_cmd" "$flag" "$remote" "$port" + cmd_remotely "$flag" "$module_cmd" "$remote" "$port" ;; esac } @@ -138,10 +146,10 @@ function convert_module_info() local unload="$1" shift local raw_modules_str="$*" - local parameters_str="" - local final_command="" - local remove_flag="" - local module_str="" + local parameters_str='' + local final_command='' + local remove_flag='' + local module_str='' local first_time=1 if [[ "$unload" == "$UNLOAD" ]]; then @@ -154,21 +162,21 @@ function convert_module_info() # Target event. e.g.: amdgpu_dm or amdgpu for module in "${modules[@]}"; do parameters_str='' - module_str="modprobe $remove_flag $module" + module_str="modprobe ${remove_flag} ${module}" if [[ "$module" =~ .*':'.* ]]; then - module_str="modprobe $remove_flag " - module_str+=$(cut -d ':' -f1 <<< "$module") + module_str="modprobe ${remove_flag} " + module_str+=$(cut --delimiter=':' --fields=1 <<< "$module") if [[ "$unload" != "$UNLOAD" ]]; then # Capture module parameters - specific_parameters_str=$(cut -d ':' -f2 <<< "$module") + specific_parameters_str=$(cut --delimiter=':' --fields=2 <<< "$module") IFS=',' read -r -a parameters_array <<< "$specific_parameters_str" for specific_parameter in "${parameters_array[@]}"; do parameters_str+="$specific_parameter " done - module_str+=" $parameters_str" + module_str+=" ${parameters_str}" fi fi @@ -177,7 +185,7 @@ function convert_module_info() first_time=0 continue fi - final_command+=" && $module_str" + final_command+=" && ${module_str}" done if [[ -z "$final_command" ]]; then @@ -208,6 +216,8 @@ function gui_control() local isolate_target local remote local port + local set_default='false' + local default_command flag=${flag:-'SILENT'} @@ -215,15 +225,34 @@ function gui_control() isolate_target='graphical.target' vt_console=1 gui_control_cmd="${configurations[gui_on]}" - else + elif [[ "$operation" == 'OFF' ]]; then isolate_target='multi-user.target' vt_console=0 gui_control_cmd="${configurations[gui_off]}" + elif [[ "$operation" == 'ON_AFTER_REBOOT' ]]; then + isolate_target='graphical.target' + vt_console=1 + gui_control_cmd="${configurations[gui_on_after_reboot]}" + set_default='true' + warning 'This option will take effect after reboot' >&2 + elif [[ "$operation" == 'OFF_AFTER_REBOOT' ]]; then + isolate_target='multi-user.target' + vt_console=0 + gui_control_cmd="${configurations[gui_off_after_reboot]}" + set_default='true' + warning 'This option will take effect after reboot' >&2 fi # If the user does not override the turn on/off command we use the default # systemctl - gui_control_cmd=${gui_control_cmd:-"systemctl isolate ${isolate_target}"} + + if [[ "$set_default" == 'true' ]]; then + default_command="systemctl set-default ${isolate_target}" + else + default_command="systemctl isolate ${isolate_target}" + fi + + gui_control_cmd=${gui_control_cmd:-"${default_command}"} bind_control_cmd='for i in /sys/class/vtconsole/*/bind; do printf "%s\n" '$vt_console' > $i; done; sleep 0.5' # is this right? case "$target" in @@ -234,8 +263,8 @@ function gui_control() cmd_manager "$flag" "$bind_control_cmd" ;; 3) # REMOTE TARGET - cmd_remotely "$gui_control_cmd" "$flag" "$remote" "$port" - cmd_remotely "$bind_control_cmd" "$flag" "$remote" "$port" '' '1' + cmd_remotely "$flag" "$gui_control_cmd" "$remote" "$port" + cmd_remotely "$flag" "$bind_control_cmd" "$remote" "$port" '' '1' ;; esac } @@ -258,52 +287,61 @@ function get_available_connectors() local remote local port local find_conn_cmd + local connector_enabled declare -A cards flag=${flag:-'SILENT'} + # command to find all cards and for each of them append if it is enabled or not + find_conn_cmd="find ${SYSFS_CLASS_DRM} -name 'card*-*' -exec printf '%s,' {} \; -exec cat {}/enabled \; -exec printf '\n' \;" + case "$target" in 2) # LOCAL TARGET - cards_raw_list=$(find "$SYSFS_CLASS_DRM" -name 'card*' | sort -d) + cards_raw_list=$(cmd_manager 'SILENT' "$find_conn_cmd" | sort --dictionary-order) if [[ -f "$SYSFS_CLASS_DRM" ]]; then ret="$?" - complain "We cannot access $SYSFS_CLASS_DRM" + complain "We cannot access ${SYSFS_CLASS_DRM}" return "$ret" # ENOENT fi target_label='local' ;; 3) # REMOTE TARGET - find_conn_cmd="find $SYSFS_CLASS_DRM -name 'card*'" - - cards_raw_list=$(cmd_remotely "$find_conn_cmd" "$flag" | sort -d) + cards_raw_list=$(cmd_remotely "$flag" "$find_conn_cmd" | sort --dictionary-order) target_label='remote' ;; esac - while read -r card; do - card=$(basename "$card") - key=$(printf '%s\n' "$card" | grep card | cut -d- -f1) - value=$(printf '%s\n' "$card" | grep card | cut -d- -f2) + while read -r card_info; do + card_path=$(printf '%s' "$card_info" | cut --delimiter=',' --fields=1) + connector_enabled=$(printf '%s' "$card_info" | cut --delimiter=',' --fields=2) + card=$(basename "$card_path") + key=$(printf '%s\n' "$card" | grep card | cut --delimiter='-' --fields=1) + value=$(printf '%s\n' "$card" | grep card | cut --delimiter='-' --fields=2-) [[ "$key" == "$value" ]] && continue if [[ -n "$key" && -n "$value" ]]; then list_of_values="${cards[$key]}" + + if [[ "$connector_enabled" == 'enabled' ]]; then + value="${value} *" + fi + if [[ -z "$list_of_values" ]]; then cards["$key"]="$value" continue fi - cards["$key"]="$list_of_values,$value" + cards["$key"]="${list_of_values},${value}" fi done <<< "$cards_raw_list" for card in "${!cards[@]}"; do connectors="${cards[$card]}" - printf '%s\n' "[$target_label] ${card^} supports:" + printf '%s\n' "[${target_label}] ${card^} supports:" IFS=',' read -r -a connectors <<< "${cards[$card]}" for conn in "${connectors[@]}"; do - printf '%s\n' " $conn" + printf '%s\n' " ${conn}" done done @@ -324,20 +362,20 @@ function get_supported_mode_per_connector() flag=${flag:-'SILENT'} - cmd="for f in $SYSFS_CLASS_DRM/*/modes;"' do c=$(< $f) && [[ ! -z $c ]] && printf "%s\n" "$f:" "$c" ""; done' + cmd="for f in ${SYSFS_CLASS_DRM}/*/modes;"' do c=$(< $f) && [[ ! -z $c ]] && printf "%s\n" "$f:" "$c" ""; done' case "$target" in 2) # LOCAL TARGET if [[ -f "$SYSFS_CLASS_DRM" ]]; then ret="$?" - complain "We cannot access $SYSFS_CLASS_DRM" + complain "We cannot access ${SYSFS_CLASS_DRM}" return "$ret" # ENOENT fi modes=$(eval "$cmd") target_label='local' ;; 3) # REMOTE TARGET - modes=$(cmd_remotely "$cmd" 'SILENT' '' '' '' '1') + modes=$(cmd_remotely 'SILENT' "$cmd" '' '' '' '1') target_label='remote' ;; esac @@ -351,8 +389,8 @@ function get_supported_mode_per_connector() function parse_drm_options() { - local long_options='remote:,local,gui-on,gui-off,load-module:,unload-module:,help,verbose' - long_options+=',conn-available,modes' + local long_options='remote:,local,gui-on,gui-off,gui-on-after-reboot,gui-off-after-reboot' + long_options+=',load-module:,unload-module:,help,verbose,conn-available,modes' local short_options='h' local raw_options="$*" local options @@ -369,6 +407,8 @@ function parse_drm_options() options_values['GUI_ON']='' options_values['GUI_OFF']='' + options_values['GUI_ON_AFTER_REBOOT']='' + options_values['GUI_OFF_AFTER_REBOOT']='' options_values['CONN_AVAILABLE']='' options_values['HELP']='' options_values['LOAD_MODULE']='' @@ -395,14 +435,14 @@ function parse_drm_options() options_values['TARGET']="$LOCAL_TARGET" fi - eval "set -- $options" + eval "set -- ${options}" while [[ "$#" -gt 0 ]]; do case "$1" in --remote) populate_remote_info "$2" if [[ "$?" == 22 ]]; then options_values['ERROR']="$option" - return 22 + return 22 # EINVAL fi options_values['TARGET']="$REMOTE_TARGET" shift 2 @@ -419,6 +459,14 @@ function parse_drm_options() options_values['GUI_OFF']=1 shift ;; + --gui-on-after-reboot) + options_values['GUI_ON_AFTER_REBOOT']=1 + shift + ;; + --gui-off-after-reboot) + options_values['GUI_OFF_AFTER_REBOOT']=1 + shift + ;; --load-module) #options_values['LOAD_MODULE']=$(cut -d '=' -f2- <<< "$option") if [[ "$2" =~ ^-- ]]; then @@ -471,7 +519,7 @@ function parse_drm_options() function drm_help() { if [[ "$1" =~ --help ]]; then - include "$KW_LIB_DIR/help.sh" + include "${KW_LIB_DIR}/help.sh" kworkflow_man 'drm' return fi @@ -480,6 +528,8 @@ function drm_help() ' drm [--local | --remote [:]] (-um|--unload-module)=[;;...]' \ ' drm [--local | --remote [:]] --gui-on' \ ' drm [--local | --remote [:]] --gui-off' \ + ' drm [--local | --remote [:]] --gui-on-after-reboot' \ + ' drm [--local | --remote [:]] --gui-off-after-reboot' \ ' drm [--local | --remote [:]] --conn-available' \ ' drm [--local | --remote [:]] --verbose' \ ' drm [--local | --remote [:]] --modes' diff --git a/src/pomodoro.sh b/src/pomodoro.sh index 419975e0b..94898d3fb 100644 --- a/src/pomodoro.sh +++ b/src/pomodoro.sh @@ -138,7 +138,7 @@ function is_tag_already_registered() is_tag_registered=$(select_from "tag WHERE name IS '${tag_name}'" '' '' '' '' "$flag") [[ -n "${is_tag_registered}" ]] && return 0 - return 1 + return 1 # EPERM } # This is the thread function that will be used to notify when the Pomodoro diff --git a/src/report.sh b/src/report.sh index 9aef38c20..24414d1ac 100644 --- a/src/report.sh +++ b/src/report.sh @@ -448,7 +448,7 @@ function parse_report_options() if [[ "$reference_count" -gt 1 ]]; then complain 'Please, only use a single time reference' - return 22 + return 22 # EINVAL elif [[ "$reference_count" == 0 ]]; then # If no option, set day as a default options_values['DAY']=$(get_today_info '+%Y/%m/%d') diff --git a/tests/integration/device_test.sh b/tests/integration/device_test.sh index 4694b247c..92dd134cf 100755 --- a/tests/integration/device_test.sh +++ b/tests/integration/device_test.sh @@ -52,7 +52,7 @@ function device_info_test_helper() expected_output=$(sed '/GPU:/q' <<< "${expected_output}" | sed '/GPU:/d') fi - assertEquals "(${LINENO}): kw device failed for ${distro}" "${output}" "${expected_output}" + assertEquals "(${LINENO}): kw device failed for ${distro}" "${expected_output}" "${output}" # check storage information fs_type=$(container_inspect --format '{{.Driver}}' "${container}") diff --git a/tests/integration/kw_version_test.sh b/tests/integration/kw_version_test.sh index 10379c656..35f42dccc 100755 --- a/tests/integration/kw_version_test.sh +++ b/tests/integration/kw_version_test.sh @@ -4,19 +4,21 @@ include './src/lib/kwio.sh' include './tests/unit/utils.sh' include './tests/integration/utils.sh' -declare -g expected_output - -function oneTimeSetUp() +# This function gets the commit hash and base version of the specified branch, +# constructs the expected output. +# +# @container: The name of the container. +# +# Return: +# Return 0: On success. +# Return 1: If it fails to get the branch name, head commit hash, or base version. +function kw_version_check_version() { - local kw_git_dir - local kw_dir + local container="$1" local head_hash local branch_name local base_version - - # git directory path - kw_dir="${KWROOT_DIR}" - kw_git_dir="${kw_dir}/.git" + local expected_output # In order to check correctness of `kw --version`, we collect some information # from the git repo: @@ -24,43 +26,69 @@ function oneTimeSetUp() # - Base version (alpha, beta, or other) # - Branch name # - Commit sha - # - # Because the local KW repo is copied to the container, we run the following - # commands directly on the host instead of running in the container. - head_hash=$(git --git-dir "${kw_git_dir}" rev-parse --short HEAD) - branch_name=$(git --git-dir "${kw_git_dir}" rev-parse --short --abbrev-ref HEAD) - base_version=$(head --lines 1 "${kw_dir}/src/VERSION") + branch_name=$(container_exec "$container" "git rev-parse --short --abbrev-ref HEAD") + if [[ "$?" -ne 0 ]]; then + complain "Failed to get the branch name" + return 1 # EPERM + fi + + head_commit_hash=$(container_exec "$container" "git rev-parse --short ${branch_name}") + if [[ "$?" -ne 0 ]]; then + complain 'Failed to get the head commit hash' + return 1 # EPERM + fi + + base_version=$(container_exec "$container" "git show ${branch_name}:./src/VERSION | head --lines 1") + if [[ "$?" -ne 0 ]]; then + complain 'Failed to get the base version' + return 1 # EPERM + fi + + expected_output=$(printf '%s\nBranch: %s\nCommit: %s' "$base_version" "$branch_name" "$head_commit_hash") - # using the gathered information, we build the expected output - expected_output=$(printf '%s\nBranch: %s\nCommit: %s' "$base_version" "$branch_name" "$head_hash") + printf '%s\n' "$expected_output" } -function kw_version_test_helper() +# Function to test the kw version feature across different distributions inside +# containers, using the three forms of the feature: kw version, kw --version, +# and kw -v. +function test_kw_version() { - local distro="$1" local container + local distro local output + local expected_output - # collect the kw version in the container - container="kw-${distro}" - output=$(container_exec "${container}" 'kw --version') + for distro in "${DISTROS[@]}"; do + container="kw-${distro}" - assertEquals "(${LINENO}): kw version failed for ${distro}" "$expected_output" "$output" -} + # collect the expected output in the container. + expected_output=$(kw_version_check_version "$container") + if [[ "$?" -ne 0 ]]; then + fail "(${LINENO}): Couldn't collect expected version output from container ${container}" + fi -function test_kw_version_on_archlinux() -{ - kw_version_test_helper 'archlinux' -} + # collect the version in the container using `kw version` + output=$(container_exec "$container" 'kw version') + if [[ "$?" -ne 0 ]]; then + fail "(${LINENO}): Failed to get 'kw version' for ${distro}" + fi + assert_equals_helper "'kw version' failed for ${distro}" "$LINENO" "$expected_output" "$output" -function test_kw_version_on_debian() -{ - kw_version_test_helper 'debian' -} + # collect the version in the container using `kw --version` + output=$(container_exec "$container" 'kw --version') + if [[ "$?" -ne 0 ]]; then + fail "(${LINENO}): Failed to get 'kw --version' for ${distro}" + fi + assert_equals_helper "'kw --version' failed for ${distro}" "$LINENO" "$expected_output" "$output" -function test_kw_version_on_fedora() -{ - kw_version_test_helper 'fedora' + # collect the version in the container using `kw -v` + output=$(container_exec "$container" 'kw -v') + if [[ "$?" -ne 0 ]]; then + fail "(${LINENO}): Failed to get 'kw -v' for ${distro}" + fi + assert_equals_helper "'kw -v' failed for ${distro}" "$LINENO" "$expected_output" "$output" + done } invoke_shunit diff --git a/tests/unit/backup_test.sh b/tests/unit/backup_test.sh index e2b5337eb..15cba66ef 100755 --- a/tests/unit/backup_test.sh +++ b/tests/unit/backup_test.sh @@ -32,7 +32,7 @@ function test_create_backup() done) output=$(create_backup /randomly/random/path) - assertEquals "$LINENO" "$output" 'We could not find the path' + assertEquals "$LINENO" 'We could not find the path' "$output" output=$(create_backup "$SHUNIT_TMPDIR" 'SILENT') filepath=$(str_remove_prefix "$output" 'Backup successfully created at ') @@ -47,7 +47,7 @@ function test_restore_backup() local output output=$(restore_backup "$SHUNIT_TMPDIR"/random/path/backup.tar.gz) - assertEquals "($LINENO)" "$output" 'We could not find this file' + assertEquals "($LINENO)" 'We could not find this file' "$output" output=$(restore_backup tests/unit/samples/kw-backup-2021-08-07_23-42-51.tar.gz) assertTrue "($LINENO) Not all files were extracted" \ @@ -83,17 +83,17 @@ function test_restore_config() config_2_last_line=$(tail -n 1 "$KW_DATA_DIR/configs/configs/config-2") output=$(printf '%s\n' 'n' | restore_config) - assertEquals "$LINENO" "$(printf '%s\n' "$output" | head -n 1)" 'It looks like that the file config-2 differs from the backup version.' + assertEquals "$LINENO" 'It looks like that the file config-2 differs from the backup version.' "$(printf '%s\n' "$output" | head -n 1)" # Since we answered no above, we expect config-2 to remain the same assert_equals_helper "config-2 should've reamined the same" "$LINENO" "$config_2_last_line" "$(tail -n 1 "$KW_DATA_DIR/configs/configs/config-2")" output=$(printf '%s\n' 'y' | restore_config) - assertEquals "$LINENO" "$(printf '%s\n' "$output" | head -n 1)" 'It looks like that the file config-2 differs from the backup version.' + assertEquals "$LINENO" 'It looks like that the file config-2 differs from the backup version.' "$(printf '%s\n' "$output" | head -n 1)" # Now config-2 should be changed, as we said yes above config_2_last_line=$(tail -n 1 "$KW_DATA_DIR/configs/configs/config-2") - assertEquals "$LINENO" "$config_2_last_line" '# This line is different' + assertEquals "$LINENO" '# This line is different' "$config_2_last_line" } function test_restore_data_from_dir() diff --git a/tests/unit/build_test.sh b/tests/unit/build_test.sh index 1c5473461..c66671a59 100755 --- a/tests/unit/build_test.sh +++ b/tests/unit/build_test.sh @@ -307,7 +307,7 @@ function test_kernel_build_invalid_flag() output=$(build_kernel_main 'TEST_MODE' --notvalid 2> /dev/null) ret="$?" - assertEquals "($LINENO)" "$ret" 22 + assertEquals "($LINENO)" 22 "$ret" } function test_kernel_build_outside_kernel_repository() @@ -322,7 +322,7 @@ function test_kernel_build_outside_kernel_repository() output=$(build_kernel_main 'TEST_MODE') ret="$?" - assert_equals_helper 'We expected an error' "($LINENO)" "$ret" 125 + assert_equals_helper 'We expected an error' "($LINENO)" 125 "$ret" cd "$FAKE_KERNEL" || { fail "($LINENO) It was not possible to move into temporary directory" @@ -861,6 +861,122 @@ function test_kernel_build_cpu_scaling_llvm_warning_sava_log_to() compare_command_sequence '' "($LINENO)" 'expected_result' "$output" } +function test_kernel_build_from_sha() +{ + local expected_result + local output + local sha='HEAD^' + + mk_fake_git + + output=$(build_kernel_main 'TEST_MODE' --from-sha ${sha}) + declare -a expected_result=( + "git rebase HEAD^ --exec 'kw build'" + ) + + compare_command_sequence '' "(${LINENO})" 'expected_result' "$output" +} + +function test_kernel_build_from_sha_nonexisting_sha() +{ + local expected_result + local output + local sha='fakesha' + + mk_fake_git + + output=$(build_kernel_main 'TEST_MODE' --from-sha ${sha}) + declare -a expected_result=( + "ERROR: The given SHA (${sha}) does not represent a valid commit sha." + ) + + compare_command_sequence '' "(${LINENO})" 'expected_result' "$output" +} + +function test_kernel_build_from_sha_sha_not_ancestor() +{ + local expected_result + local output + + mk_fake_git + mk_git_branch 'branch' + + output=$(build_kernel_main 'TEST_MODE' --from-sha branch) + declare -a expected_result=( + 'ERROR: Given SHA (branch) is invalid. Check if it is an ancestor of the branch head.' + ) + + compare_command_sequence '' "(${LINENO})" 'expected_result' "$output" +} + +function test_kernel_build_from_sha_pending_rebase() +{ + local expected_result + local output + local sha='HEAD^' + + mk_fake_git + mkdir '.git/rebase-merge' + + output=$(build_kernel_main 'TEST_MODE' --from-sha ${sha}) + declare -a expected_result=( + 'ERROR: Abort the repository rebase before continuing with build from sha (use "git rebase --abort")!' + ) + + compare_command_sequence '' "(${LINENO})" 'expected_result' "$output" +} + +function test_kernel_build_from_sha_pending_merge() +{ + local expected_result + local output + local sha='HEAD^' + + mk_fake_git + touch '.git/MERGE_HEAD' + + output=$(build_kernel_main 'TEST_MODE' --from-sha ${sha}) + declare -a expected_result=( + 'ERROR: Abort the repository merge before continuing with build from sha (use "git rebase --abort")!' + ) + + compare_command_sequence '' "(${LINENO})" 'expected_result' "$output" +} + +function test_kernel_build_from_sha_pending_bisect() +{ + local expected_result + local output + local sha='HEAD^' + + mk_fake_git + touch '.git/BISECT_LOG' + + output=$(build_kernel_main 'TEST_MODE' --from-sha ${sha}) + declare -a expected_result=( + 'ERROR: Stop the repository bisect before continuing with build from sha (use "git bisect reset")!' + ) + + compare_command_sequence '' "(${LINENO})" 'expected_result' "$output" +} + +function test_kernel_build_from_sha_pending_apply() +{ + local expected_result + local output + local sha='HEAD^' + + mk_fake_git + mkdir '.git/rebase-apply' + + output=$(build_kernel_main 'TEST_MODE' --from-sha ${sha}) + declare -a expected_result=( + 'ERROR: Abort the repository patch apply before continuing with build from sha (use "git am --abort")!' + ) + + compare_command_sequence '' "(${LINENO})" 'expected_result' "$output" +} + function test_kernel_build_inside_an_env() { local output diff --git a/tests/unit/config_test.sh b/tests/unit/config_test.sh index ca4b2922a..9a3c1a61e 100755 --- a/tests/unit/config_test.sh +++ b/tests/unit/config_test.sh @@ -46,29 +46,29 @@ function show_raw_configurations() function test_is_config_file_valid() { is_config_file_valid 'invalid' - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" is_config_file_valid 'builds' - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" is_config_file_valid 'kworkflows' - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" # Valid options is_config_file_valid 'kworkflow' - assertEquals "($LINENO)" "$?" 0 + assertEquals "($LINENO)" 0 "$?" is_config_file_valid 'build' - assertEquals "($LINENO)" "$?" 0 + assertEquals "($LINENO)" 0 "$?" } function test_is_a_valid_config_option_only_valid_options() { is_a_valid_config_option 'build' 'cross_compile' - assertEquals "($LINENO)" "$?" 0 + assertEquals "($LINENO)" 0 "$?" is_a_valid_config_option 'kworkflow' 'ssh_ip' - assertEquals "($LINENO)" "$?" 0 + assertEquals "($LINENO)" 0 "$?" } function test_is_a_valid_config_invalid_parameters() @@ -76,16 +76,16 @@ function test_is_a_valid_config_invalid_parameters() local output output=$(is_a_valid_config_option 'build') - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" output=$(is_a_valid_config_option 'kworkflow') - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" output=$(is_a_valid_config_option 'kworkflow' 'this_is_invalid') - assertEquals "($LINENO)" "$?" 95 + assertEquals "($LINENO)" 95 "$?" output=$(is_a_valid_config_option 'build' 'nop') - assertEquals "($LINENO)" "$?" 95 + assertEquals "($LINENO)" 95 "$?" } function test_set_config_check_if_file_still_a_link_after_change() @@ -118,13 +118,13 @@ function test_set_config_value_changing_default_value() function test_set_config_value_with_dot_in_the_value() { validate_option_parameter 'this.is valid' - assertEquals "($LINENO)" "$?" 0 + assertEquals "($LINENO)" 0 "$?" validate_option_parameter 'this.is valid.also.valid' - assertEquals "($LINENO)" "$?" 0 + assertEquals "($LINENO)" 0 "$?" validate_option_parameter 'this is.not.valid' - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" } function test_set_config_with_a_path_as_value() @@ -151,10 +151,10 @@ function test_set_config_with_verbose_mode() function test_check_if_target_config_exist() { check_if_target_config_exist 'vm' 'vm.config' - assertEquals "($LINENO)" "$?" 0 + assertEquals "($LINENO)" 0 "$?" check_if_target_config_exist 'vm' 'la.config' - assertEquals "($LINENO)" "$?" 2 + assertEquals "($LINENO)" 2 "$?" } function test_parse_config_options() @@ -292,9 +292,9 @@ function test_show_configurations_invalid_target() local output output=$(show_configurations invalid_target) - assertEquals "($LINENO)" "$output" 'Invalid config target: invalid_target' + assertEquals "($LINENO)" 'Invalid config target: invalid_target' "$output" show_configurations invalid_target > /dev/null 2>&1 - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" } invoke_shunit diff --git a/tests/unit/debug_test.sh b/tests/unit/debug_test.sh index f2dca859e..681de8584 100755 --- a/tests/unit/debug_test.sh +++ b/tests/unit/debug_test.sh @@ -120,7 +120,7 @@ function test_process_list() raw_input='' output=$(process_list "$raw_input" 1) ret="$?" - assert_equals_helper 'Return error:' "($LINENO)" "$ret" 22 + assert_equals_helper 'Return error:' "($LINENO)" 22 "$ret" } function test_convert_event_syntax_to_sys_path_hash() @@ -223,8 +223,8 @@ function test_convert_event_syntax_to_sys_path_hash() IFS=$'\n' printf '%s\n' "${!events_hash[*]}" ) - assert_equals_helper 'Wrong syntax:' "($LINENO)" "$output" '' - assert_equals_helper 'Return error:' "($LINENO)" "$ret" 22 + assert_equals_helper 'Wrong syntax:' "($LINENO)" '' "$output" + assert_equals_helper 'Return error:' "($LINENO)" 22 "$ret" events_hash=() convert_event_syntax_to_sys_path_hash ':' @@ -233,8 +233,8 @@ function test_convert_event_syntax_to_sys_path_hash() IFS=$'\n' printf '%s\n' "${!events_hash[*]}" ) - assert_equals_helper 'Wrong syntax:' "($LINENO)" "$output" '' - assert_equals_helper 'Return error:' "($LINENO)" "$ret" 22 + assert_equals_helper 'Wrong syntax:' "($LINENO)" '' "$output" + assert_equals_helper 'Return error:' "($LINENO)" 22 "$ret" } function test_build_event_command_string() @@ -311,9 +311,9 @@ function test_parser_debug_options_remote() { # 1) Parser remote without config file. parser_debug_options --remote 'juca@localhost:33' - assert_equals_helper 'Expected localhost' "$LINENO" "${remote_parameters['REMOTE_IP']}" 'localhost' - assert_equals_helper 'Expected port 33' "$LINENO" "${remote_parameters['REMOTE_PORT']}" 33 - assert_equals_helper 'Expected user' "$LINENO" "${remote_parameters['REMOTE_USER']}" 'juca' + assert_equals_helper 'Expected localhost' "$LINENO" 'localhost' "${remote_parameters['REMOTE_IP']}" + assert_equals_helper 'Expected port 33' "$LINENO" 33 "${remote_parameters['REMOTE_PORT']}" + assert_equals_helper 'Expected user' "$LINENO" 'juca' "${remote_parameters['REMOTE_USER']}" # 2) Parser remote with config file @@ -329,16 +329,16 @@ function test_parser_debug_options_remote() # 2.1) We have a config file, with origin set as host, and user request it parser_debug_options --remote 'origin' - assert_equals_helper 'Expected origin' "$LINENO" "${remote_parameters['REMOTE_FILE_HOST']}" 'origin' + assert_equals_helper 'Expected origin' "$LINENO" 'origin' "${remote_parameters['REMOTE_FILE_HOST']}" # 2.2) We have a config file, with --remote NAME, but we don't have NAME in the # config file. parser_debug_options --remote 'debian-test-dns' - assert_equals_helper 'Expected empty' "$LINENO" "${remote_parameters['REMOTE_FILE_HOST']}" '' - assert_equals_helper 'Expected debian-test-dns' "$LINENO" "${remote_parameters['REMOTE_IP']}" 'debian-test-dns' + assert_equals_helper 'Expected empty' "$LINENO" '' "${remote_parameters['REMOTE_FILE_HOST']}" + assert_equals_helper 'Expected debian-test-dns' "$LINENO" 'debian-test-dns' "${remote_parameters['REMOTE_IP']}" parser_debug_options --remote '192.0.2.0' - assert_equals_helper 'Expected 192.0.2.0' "$LINENO" "${remote_parameters['REMOTE_IP']}" '192.0.2.0' + assert_equals_helper 'Expected 192.0.2.0' "$LINENO" '192.0.2.0' "${remote_parameters['REMOTE_IP']}" cd "$original_dir" || { fail "($LINENO) It was not possible to move back to original directory" @@ -353,62 +353,62 @@ function test_parser_debug_options() # Validate list option parser_debug_options --list - assert_equals_helper 'Expected list' "$LINENO" "${options_values['LIST']}" 1 + assert_equals_helper 'Expected list' "$LINENO" 1 "${options_values['LIST']}" parser_debug_options --list --event 'amdgpu_dm' - assert_equals_helper 'Expected amdgpu_dm' "$LINENO" "${options_values['EVENT']}" 'amdgpu_dm' + assert_equals_helper 'Expected amdgpu_dm' "$LINENO" 'amdgpu_dm' "${options_values['EVENT']}" # Validate history option parser_debug_options --history - assert_equals_helper 'Expected history' "$LINENO" "${options_values['HISTORY']}" 1 + assert_equals_helper 'Expected history' "$LINENO" 1 "${options_values['HISTORY']}" # Validate follow parser_debug_options --follow - assert_equals_helper 'Expected follow' "$LINENO" "${options_values['FOLLOW']}" 1 + assert_equals_helper 'Expected follow' "$LINENO" 1 "${options_values['FOLLOW']}" # Validate event event_str='amdgpu_dm:dc_something[x>3]' parser_debug_options --event "$event_str" - assert_equals_helper 'Expected event' "$LINENO" "${options_values['EVENT']}" "$event_str" + assert_equals_helper 'Expected event' "$LINENO" "$event_str" "${options_values['EVENT']}" # Validate disable event parser_debug_options --event "$event_str" --disable - assert_equals_helper 'Expected event' "$LINENO" "${options_values['DISABLE']}" 1 + assert_equals_helper 'Expected event' "$LINENO" 1 "${options_values['DISABLE']}" # Validate disable event parser_debug_options --event "$event_str" --cmd "$fake_cmd" - assert_equals_helper 'Expected event' "$LINENO" "${options_values['CMD']}" "$fake_cmd" + assert_equals_helper 'Expected event' "$LINENO" "$fake_cmd" "${options_values['CMD']}" # Check local option parser_debug_options --local --event "$event_str" --disable - assert_equals_helper 'Expected event' "$LINENO" "${options_values['TARGET']}" 2 + assert_equals_helper 'Expected event' "$LINENO" 2 "${options_values['TARGET']}" # Check test_mode parser_debug_options test_mode - assert_equals_helper 'Expected event' "$LINENO" "${options_values['TEST_MODE']}" 'TEST_MODE' + assert_equals_helper 'Expected event' "$LINENO" 'TEST_MODE' "${options_values['TEST_MODE']}" # Validate dmesg parser_debug_options --dmesg - assert_equals_helper 'Expected dmesg' "$LINENO" "${options_values['DMESG']}" 1 + assert_equals_helper 'Expected dmesg' "$LINENO" 1 "${options_values['DMESG']}" # Validate ftrace parser_debug_options --ftrace - assert_equals_helper 'Expected ftrace failure' "$LINENO" "$?" 22 + assert_equals_helper 'Expected ftrace failure' "$LINENO" 22 "$?" parser_debug_options --list --ftrace - assert_equals_helper 'Expected ftrace failure' "$LINENO" "$?" 22 + assert_equals_helper 'Expected ftrace failure' "$LINENO" 22 "$?" ftrace_str='something:another,thing' parser_debug_options --ftrace="$ftrace_str" - assert_equals_helper 'Expected ftrace syntax' "$LINENO" "${options_values['FTRACE']}" "$ftrace_str" + assert_equals_helper 'Expected ftrace syntax' "$LINENO" "$ftrace_str" "${options_values['FTRACE']}" ftrace_str='something:' parser_debug_options --ftrace="$ftrace_str" - assert_equals_helper 'Expected ftrace setup' "$LINENO" "${options_values['FTRACE']}" "$ftrace_str" + assert_equals_helper 'Expected ftrace setup' "$LINENO" "$ftrace_str" "${options_values['FTRACE']}" ftrace_str='something: la, llu, xpto' parser_debug_options --ftrace="$ftrace_str" - assert_equals_helper 'Expected ftrace string' "$LINENO" "${options_values['FTRACE']}" "$ftrace_str" + assert_equals_helper 'Expected ftrace string' "$LINENO" "$ftrace_str" "${options_values['FTRACE']}" } function test_build_ftrace_command_string() @@ -423,10 +423,10 @@ function test_build_ftrace_command_string() # function_graph output=$(build_ftrace_command_string 'function_graph') expected_cmd="$disable_trace && printf '%s' 'function_graph' > $current_tracer && $enable_trace" - assert_equals_helper 'Expected to enable function_graph' "$LINENO" "$output" "$expected_cmd" + assert_equals_helper 'Expected to enable function_graph' "$LINENO" "$expected_cmd" "$output" output=$(build_ftrace_command_string ' function_graph ') - assert_equals_helper 'Expected to enable function_graph' "$LINENO" "$output" "$expected_cmd" + assert_equals_helper 'Expected to enable function_graph' "$LINENO" "$expected_cmd" "$output" # function_graph: -> Should fail output=$(build_ftrace_command_string 'function_graph: ') @@ -443,7 +443,7 @@ function test_build_ftrace_command_string() expected_cmd="$disable_trace && printf '%s' 'function_graph' > $current_tracer" expected_cmd+=" && printf '%s' 'amdgpu_dm*' >> $ftracer_filter" expected_cmd+=" && $enable_trace" - assert_equals_helper 'Expected amdgpu_dm filters' "$LINENO" "$output" "$expected_cmd" + assert_equals_helper 'Expected amdgpu_dm filters' "$LINENO" "$expected_cmd" "$output" # function_graph:amdgpu_dm*,dc_*,drm_test output=$(build_ftrace_command_string 'function_graph:amdgpu_dm*,dc_*,drm_test') @@ -452,7 +452,7 @@ function test_build_ftrace_command_string() expected_cmd+=" && printf '%s' 'dc_*' >> $ftracer_filter" expected_cmd+=" && printf '%s' 'drm_test' >> $ftracer_filter" expected_cmd+=" && $enable_trace" - assert_equals_helper 'Expected to find multiple filters' "$LINENO" "$output" "$expected_cmd" + assert_equals_helper 'Expected to find multiple filters' "$LINENO" "$expected_cmd" "$output" # function_graph: amdgpu_dm*, dc_* output=$(build_ftrace_command_string 'function_graph: amdgpu_dm*, dc_*') @@ -460,7 +460,7 @@ function test_build_ftrace_command_string() expected_cmd+=" && printf '%s' 'amdgpu_dm*' >> $ftracer_filter" expected_cmd+=" && printf '%s' 'dc_*' >> $ftracer_filter" expected_cmd+=" && $enable_trace" - assert_equals_helper 'Expected to find multiple filters' "$LINENO" "$output" "$expected_cmd" + assert_equals_helper 'Expected to find multiple filters' "$LINENO" "$expected_cmd" "$output" # Empty output=$(build_ftrace_command_string '') @@ -470,7 +470,7 @@ function test_build_ftrace_command_string() # Disable output=$(build_ftrace_command_string 'function_graph:amdgpu_dm*' 1) expected_cmd="$disable_trace && printf '' > $ftracer_filter && printf 'nop' > $FTRACE_CURRENT_PATH" - assert_equals_helper 'Expected disable command' "$LINENO" "$output" "$expected_cmd" + assert_equals_helper 'Expected disable command' "$LINENO" "$expected_cmd" "$output" } function test_ftrace_debug() @@ -491,7 +491,7 @@ function test_ftrace_debug() output=$(ftrace_debug 2 'TEST_MODE' 'function_graph:amdgpu_dm*') expected_cmd="$disable_trace && printf '%s' 'function_graph' > $current_tracer" expected_cmd+=" && printf '%s' 'amdgpu_dm*' >> $ftracer_filter && $enable_trace" - assert_equals_helper 'Expected command' "$LINENO" "$output" "$expected_cmd" + assert_equals_helper 'Expected command' "$LINENO" "$expected_cmd" "$output" expected_cmd_base="$expected_cmd" @@ -502,12 +502,12 @@ function test_ftrace_debug() output=$(ftrace_debug 3 'TEST_MODE' 'function_graph:amdgpu_dm*') expected_cmd="ssh -p 3333 juca@127.0.0.1 sudo \"$expected_cmd_base\"" - assert_equals_helper 'Expected remote command' "$LINENO" "$output" "$expected_cmd" + assert_equals_helper 'Expected remote command' "$LINENO" "$expected_cmd" "$output" # Follow output=$(ftrace_debug 3 'TEST_MODE' 'function_graph:amdgpu_dm*' '' 1) expected_cmd="ssh -p 3333 juca@127.0.0.1 sudo \"$expected_cmd_base && cat $trace_pipe\"" - assert_equals_helper 'Expected follow' "$LINENO" "$output" "$expected_cmd" + assert_equals_helper 'Expected follow' "$LINENO" "$expected_cmd" "$output" # History cd "$SHUNIT_TMPDIR" || { @@ -763,7 +763,7 @@ function test_event_debug() # Failure case output=$(event_debug 3 'TEST_MODE' 'lala;:') ret="$?" - assert_equals_helper 'Invalid syntax' "$LINENO" "$ret" 22 + assert_equals_helper 'Invalid syntax' "$LINENO" 22 "$ret" # List output=$(event_debug 2 'TEST_MODE' 'amdgpu_dm' '' '' '' 1) diff --git a/tests/unit/diff_test.sh b/tests/unit/diff_test.sh index 729bbc3b2..ab9c4adcc 100755 --- a/tests/unit/diff_test.sh +++ b/tests/unit/diff_test.sh @@ -30,7 +30,7 @@ function test_diff_folders() # TODO: We need to investigate this LANG part. Ideally, we don't want it here output=$(LANG=en_US.UTF-8 diff_folders "$folder_1" "$folder_2") - assertEquals "$output" "Only in ${folder_1}: get_maintainer.pl" + assertEquals "Only in ${folder_1}: get_maintainer.pl" "$output" } function test_diff_folders_no_difference() @@ -39,7 +39,7 @@ function test_diff_folders_no_difference() local folder_2="${SAMPLES_DIR}/db_files" output=$(diff_folders "$folder_1" "$folder_2") - assertEquals "$output" "" + assertEquals "" "$output" } function test_diff_folders_invalid_path() diff --git a/tests/unit/drm_plugin_test.sh b/tests/unit/drm_plugin_test.sh index 1bc583fbe..d76e41514 100755 --- a/tests/unit/drm_plugin_test.sh +++ b/tests/unit/drm_plugin_test.sh @@ -26,6 +26,9 @@ function setUp() # Parser default config file for the average case parse_configuration "$KW_CONFIG_SAMPLE" + + export bind_cmd='for i in /sys/class/vtconsole/*/bind; do printf "%s\n" 1 > $i; done; sleep 0.5' + export unbind_cmd='for i in /sys/class/vtconsole/*/bind; do printf "%s\n" 0 > $i; done; sleep 0.5' } function tearDown() @@ -63,10 +66,13 @@ function mk_fake_sys_class_drm() for dir in "${fake_dirs[@]}"; do mkdir -p "${FAKE_DRM_SYSFS}/${dir}" + printf disabled > "${FAKE_DRM_SYSFS}/${dir}/enabled" done touch "${FAKE_DRM_SYSFS}/version" touch "${FAKE_DRM_SYSFS}/card0-DP-3/modes" + printf enabled > "${FAKE_DRM_SYSFS}/card0-DVI-D-1/enabled" + printf enabled > "${FAKE_DRM_SYSFS}/card1-HDMI-A-2/enabled" cat << END >> "${FAKE_DRM_SYSFS}/card0-DP-3/modes" 1920x2160 @@ -110,6 +116,8 @@ function test_drm_parser_options() assertEquals "($LINENO)" '' "${options_values['GUI_ON']}" assertEquals "($LINENO)" '' "${options_values['GUI_OFF']}" + assertEquals "${LINENO}" '' "${options_values['GUI_ON_AFTER_REBOOT']}" + assertEquals "${LINENO}" '' "${options_values['GUI_OFF_AFTER_REBOOT']}" assertEquals "($LINENO)" '' "${options_values['CONN_AVAILABLE']}" assertEquals "($LINENO)" '' "${options_values['MODES_AVAILABLE']}" @@ -119,8 +127,14 @@ function test_drm_parser_options() parse_drm_options --gui-off assertEquals "($LINENO)" 1 "${options_values['GUI_OFF']}" + parse_drm_options --gui-on-after-reboot + assertEquals "${LINENO}" 1 "${options_values['GUI_ON_AFTER_REBOOT']}" + + parse_drm_options --gui-off-after-reboot + assertEquals "${LINENO}" 1 "${options_values['GUI_OFF_AFTER_REBOOT']}" + parse_drm_options --verbose - assertEquals "($LINENO)" 1 "${options_values['VERBOSE']}" + assertEquals "${LINENO}" 1 "${options_values['VERBOSE']}" parse_drm_options --conn-available assertEquals "($LINENO)" 1 "${options_values['CONN_AVAILABLE']}" @@ -159,8 +173,6 @@ function test_gui_control_remote() { local gui_on_cmd='systemctl isolate graphical.target' local gui_off_cmd='systemctl isolate multi-user.target' - local bind_cmd='for i in /sys/class/vtconsole/*/bind; do printf "%s\n" 1 > $i; done; sleep 0.5' - local unbind_cmd='for i in /sys/class/vtconsole/*/bind; do printf "%s\n" 0 > $i; done; sleep 0.5' local output # Remote @@ -170,7 +182,7 @@ function test_gui_control_remote() tearDown # We want to test the default cases first # REMOTE = 3 - ssh_part="ssh -p 8888 juca@127.0.0.1" + ssh_part='ssh -p 8888 juca@127.0.0.1' full_turn_on_gui_cmd="${ssh_part} sudo \"${gui_on_cmd}\"" full_bind_cmd="${ssh_part} 'sudo bash -c '\''${bind_cmd}'\'" @@ -225,16 +237,113 @@ function test_gui_control_remote() compare_command_sequence '' "$LINENO" 'expected_cmd_seq' "$output" } +function test_gui_control_on_after_reboot_local() +{ + local gui_on_after_reboot_cmd='systemctl set-default graphical.target' + local output + + # Remote + remote_parameters['REMOTE_IP']='127.0.0.1' + remote_parameters['REMOTE_PORT']='8888' + remote_parameters['REMOTE_USER']='juca' + + tearDown # We want to test the default cases first + + ssh_part='ssh -p 8888 juca@127.0.0.1' + full_turn_on_gui_after_reboot_cmd="${ssh_part} sudo \"${gui_on_after_reboot_cmd}\"" + full_bind_cmd="${ssh_part} 'sudo bash -c '\''${bind_cmd}'\'" + + declare -a expected_cmd_seq=( + "$full_turn_on_gui_after_reboot_cmd" + "$full_bind_cmd" + ) + + output=$(gui_control 'ON_AFTER_REBOOT' '3' '127.0.0.1:8888' 'TEST_MODE') + compare_command_sequence '' "${LINENO}" 'expected_cmd_seq' "${output}" + + # Test with config file + parse_configuration "$KW_CONFIG_SAMPLE" + + # Remote + remote_parameters['REMOTE_PORT']='3333' + + ssh_part="ssh -p 3333 juca@127.0.0.1" + + gui_on_after_reboot_cmd='turn on after reboot' + full_turn_on_gui_after_reboot_cmd="${ssh_part} sudo \"${gui_on_after_reboot_cmd}\"" + full_bind_cmd="${ssh_part} 'sudo bash -c '\''${bind_cmd}'\'" + + declare -a expected_cmd_seq=( + "$full_turn_on_gui_after_reboot_cmd" + "$full_bind_cmd" + ) + + output=$(gui_control 'ON_AFTER_REBOOT' '3' '' 'TEST_MODE') + compare_command_sequence '' "${LINENO}" 'expected_cmd_seq' "${output}" + +} + +function test_gui_control_off_after_reboot_local() +{ + local gui_off_after_reboot_cmd='systemctl set-default multi-user.target' + local output + + # Remote + remote_parameters['REMOTE_IP']='127.0.0.1' + remote_parameters['REMOTE_PORT']='8888' + remote_parameters['REMOTE_USER']='juca' + + tearDown # We want to test the default cases first + + ssh_part='ssh -p 8888 juca@127.0.0.1' + + full_turn_off_gui_after_reboot_cmd="${ssh_part} sudo \"${gui_off_after_reboot_cmd}\"" + full_unbind_cmd="$ssh_part 'sudo bash -c '\''${unbind_cmd}'\'" + + declare -a expected_cmd_seq=( + "$full_turn_off_gui_after_reboot_cmd" + "$full_unbind_cmd" + ) + + output=$(gui_control 'OFF_AFTER_REBOOT' '3' '127.0.0.1:8888' 'TEST_MODE') + compare_command_sequence '' "${LINENO}" 'expected_cmd_seq' "${output}" + + # Test with config file + parse_configuration "$KW_CONFIG_SAMPLE" + + # Remote + remote_parameters['REMOTE_PORT']='3333' + + ssh_part="ssh -p 3333 juca@127.0.0.1" + + gui_off_after_reboot_cmd='turn off after reboot' + full_turn_off_gui_after_reboot_cmd="${ssh_part} sudo \"${gui_off_after_reboot_cmd}\"" + full_unbind_cmd="${ssh_part} 'sudo bash -c '\''${unbind_cmd}'\'" + + declare -a expected_cmd_seq=( + "$full_turn_off_gui_after_reboot_cmd" + "$full_unbind_cmd" + ) + + output=$(gui_control 'OFF_AFTER_REBOOT' '3' '' 'TEST_MODE') + compare_command_sequence '' "${LINENO}" 'expected_cmd_seq' "${output}" + +} + function test_gui_control_local() { local gui_on_cmd='systemctl isolate graphical.target' local gui_off_cmd='systemctl isolate multi-user.target' + local gui_on_after_reboot_cmd='systemctl set-default graphical.target' + local gui_off_after_reboot_cmd='systemctl set-default multi-user.target' local bind_cmd='for i in /sys/class/vtconsole/*/bind; do printf "%s\n" 1 > $i; done; sleep 0.5' local unbind_cmd='for i in /sys/class/vtconsole/*/bind; do printf "%s\n" 0 > $i; done; sleep 0.5' local output configurations[gui_on]="$gui_on_cmd" configurations[gui_off]="$gui_off_cmd" + configurations[gui_on_after_reboot]="$gui_on_after_reboot_cmd" + configurations[gui_off_after_reboot]="$gui_off_after_reboot_cmd" declare -a expected_cmd_seq=( "sudo ${gui_on_cmd}" @@ -251,6 +360,25 @@ function test_gui_control_local() output=$(gui_control 'OFF' '2' '' 'TEST_MODE') compare_command_sequence '' "$LINENO" 'expected_cmd_seq' "$output" + + # Test GUI ON after reboot + declare -a expected_cmd_seq=( + "sudo ${gui_on_after_reboot_cmd}" + "sudo ${bind_cmd}" + ) + + output=$(gui_control 'ON_AFTER_REBOOT' '2' '' 'TEST_MODE') + compare_command_sequence '' "$LINENO" 'expected_cmd_seq' "$output" + + # Test GUI OFF after reboot + declare -a expected_cmd_seq=( + "sudo ${gui_off_after_reboot_cmd}" + "sudo ${unbind_cmd}" + ) + + output=$(gui_control 'OFF_AFTER_REBOOT' '2' '' 'TEST_MODE') + compare_command_sequence '' "$LINENO" 'expected_cmd_seq' "$output" + } function test_get_available_connectors_local() @@ -259,16 +387,16 @@ function test_get_available_connectors_local() declare -a expected_output=( '[local] Card1 supports:' - 'DP' - 'DP' - 'DP' - 'HDMI' + 'DP-4' + 'DP-5' + 'DP-6' + 'HDMI-A-2 *' '[local] Card0 supports:' - 'DP' - 'DP' - 'DP' - 'DVI' - 'HDMI' + 'DP-1' + 'DP-2' + 'DP-3' + 'DVI-D-1 *' + 'HDMI-A-1' ) # Local @@ -276,6 +404,7 @@ function test_get_available_connectors_local() compare_command_sequence '' "$LINENO" 'expected_output' "$output" } +# shellcheck disable=SC2317 # Disable shellchek warning about unreachable commands in this function function test_get_available_connectors_remote() { local output @@ -284,16 +413,16 @@ function test_get_available_connectors_remote() output=$( function cmd_remotely() { - printf '/sys/class/drm/card0-%s-1\n' 'DP' - printf '/sys/class/drm/card0-%s-1\n' 'eDP' + printf '/sys/class/drm/card0-%s,enabled\n' 'DP-1' + printf '/sys/class/drm/card0-%s,disabled\n' 'eDP-1' } get_available_connectors '3' '' 'TEST_MODE' ) declare -a expected_output=( '[remote] Card0 supports:' - 'DP' - 'eDP' + 'DP-1 *' + 'eDP-1' ) compare_command_sequence '' "$LINENO" 'expected_output' "$output" } @@ -396,7 +525,7 @@ function test_convert_module_info() assertEquals "$LINENO" "$expected" "$output" output=$(convert_module_info "LOAD" "") - assertEquals "$LINENO" "$?" "22" + assertEquals "$LINENO" 22 "$?" } invoke_shunit diff --git a/tests/unit/explore_test.sh b/tests/unit/explore_test.sh index 298fdf574..1f3555ec9 100755 --- a/tests/unit/explore_test.sh +++ b/tests/unit/explore_test.sh @@ -67,9 +67,14 @@ function test_explore_files_under_git_repo() assertEquals "($LINENO)" "$MSG_OUT" "$output" output=$(explore_main 'GNU grep' '.' 'TEST_MODE') - expected_result="git grep -e 'GNU grep' -nI ." + expected_result="git grep --context 0 -e 'GNU grep' --line-number -I ." assertEquals "($LINENO)" "$expected_result" "$output" + # Test for non zero context value + output=$(explore_main --show-context=5 'GNU grep' '.' 'TEST_MODE') + expected_result="git grep --context 5 -e 'GNU grep' --line-number -I ." + assertEquals "(${LINENO})" "$expected_result" "$output" + # Test if search only in files under git control cp "$current_path/tests/unit/samples/grep_check.c" ./ MSG_OUT='GNU grep' @@ -82,13 +87,13 @@ function test_explore_files_under_git_repo() # Test only-source and only-header MSG_OUT='3' - output=$(explore_main 'camelCase' | wc -l) + output=$(explore_main 'camelCase' | wc --lines) assertEquals "($LINENO)" "$MSG_OUT" "$output" MSG_OUT='2' - output=$(explore_main -c 'camelCase' | wc -l) + output=$(explore_main -c 'camelCase' | wc --lines) assertEquals "($LINENO)" "$MSG_OUT" "$output" MSG_OUT='1' - output=$(explore_main -H 'camelCase' | wc -l) + output=$(explore_main -H 'camelCase' | wc --lines) assertEquals "($LINENO)" "$MSG_OUT" "$output" cd "$current_path" || { @@ -133,7 +138,11 @@ function test_explore_grep() assertEquals "($LINENO)" '.git' "$output" output=$(explore_main --grep 'GNU grep' '.' 'TEST_MODE') - expected_result="grep --color -nrI . -e 'GNU grep'" + expected_result="grep --color --line-number --recursive -I . --context 0 -e 'GNU grep'" + assertEquals "(${LINENO})" "$expected_result" "$output" + + output=$(explore_main --grep --show-context=5 'GNU grep' '.' 'TEST_MODE') + expected_result="grep --color --line-number --recursive -I . --context 5 -e 'GNU grep'" assertEquals "($LINENO)" "$expected_result" "$output" cd "$current_path" || { @@ -153,7 +162,11 @@ function test_explore_git() } output=$(explore_main --all 'GNU grep' '.' 'TEST_MODE') - expected_result="git grep --no-index -e 'GNU grep' -nI ." + expected_result="git grep --no-index --context 0 -e 'GNU grep' --line-number -I ." + assertEquals "(${LINENO})" "$expected_result" "$output" + + output=$(explore_main --all --show-context=5 'GNU grep' '.' 'TEST_MODE') + expected_result="git grep --no-index --context 5 -e 'GNU grep' --line-number -I ." assertEquals "($LINENO)" "$expected_result" "$output" # Test if the search ignores files in .git @@ -172,6 +185,47 @@ function test_explore_git() } } +function test_explore_context() +{ + local -r current_path="$PWD" + local expected_context='3' + local expected_match='avoid' + local msg_out='7' + + cd "$SHUNIT_TMPDIR" || { + fail "(${LINENO}) It was not possible to move to temporary directory" + return + } + + # Check the number of output lines + output=$(explore_main --show-context=3 'avoid' codestyle_error.c | wc --lines) + assertEquals "(${LINENO})" "$msg_out" "$output" + + output=$(explore_main --show-context="$expected_context" "$expected_match" codestyle_error.c) + + # Check if the expected match and context lines are present in the output + assert_substring_match 'Expected match not found!' "${LINENO}" "${expected_match}" "$output" + + # Check context lines below the match + for ((i = 1; i <= expected_context; i++)); do + CONTEXT_LINE=$((i + 4)) # Assuming match is on line 4 + CONTEXT_LINE_CONTENT=$(printf '%s' "$output" | head -n "${CONTEXT_LINE}" | tail --lines 1) + assert_line_match "Context line ${CONTEXT_LINE} below match" "$CONTEXT_LINE_CONTENT" "$output" + done + + # Check context lines above the match + for ((i = 1; i <= expected_context; i++)); do + CONTEXT_LINE=$((4 - i)) # Assuming match is on line 4 + CONTEXT_LINE_CONTENT=$(printf '%s' "$output" | head -n ${CONTEXT_LINE} | tail -n 1) + assert_line_match "Context line ${CONTEXT_LINE} above match" "$CONTEXT_LINE_CONTENT" "$output" + done + + cd "$current_path" || { + fail "(${LINENO}) It was not possible to move back from temp directory" + return + } +} + function test_parse_explore_options() { # Expected behaviour @@ -236,6 +290,20 @@ function test_parse_explore_options() assertEquals "($LINENO)" '0' "$ret" assertEquals "($LINENO)" 'HEADER' "${options_values['SCOPE']}" + unset options_values + declare -gA options_values + parse_explore_options -C + ret="$?" + assertEquals "(${LINENO})" '0' "$ret" + assertEquals "(${LINENO})" '3' "${options_values['CONTEXT']}" + + unset options_values + declare -gA options_values + parse_explore_options --show-context=5 + ret="$?" + assertEquals "(${LINENO})" '0' "$ret" + assertEquals "(${LINENO})" '5' "${options_values['CONTEXT']}" + # Others parse_explore_options --logljkl ret="$?" @@ -266,6 +334,13 @@ function test_parse_explore_options() assertEquals "($LINENO)" '22' "$ret" assertEquals "($LINENO)" 'Invalid arguments: Multiple search type!' "${options_values['ERROR']}" + unset options_values + declare -gA options_values + parse_explore_options --show-context=invalid + ret="$?" + assertEquals "($LINENO)" '22' "$ret" + assertEquals "($LINENO)" 'Context value must be a non-negative integer!' "${options_values['ERROR']}" + parse_explore_options main ret="$?" assertEquals "($LINENO)" '0' "$ret" diff --git a/tests/unit/help_test.sh b/tests/unit/help_test.sh index 04e30f55e..29ebacd5c 100755 --- a/tests/unit/help_test.sh +++ b/tests/unit/help_test.sh @@ -28,7 +28,7 @@ function test_kworkflow_man() expect="Couldn't find the man page for kw-error!" output=$(kworkflow_man 'error' 'TEST_MODE') ret="$?" - assertEquals "($LINENO) We expected an error." "$ret" 2 + assertEquals "($LINENO) We expected an error." 2 "$ret" assertEquals "($LINENO) We expected an error message." "$expect" "$output" } diff --git a/tests/unit/kw_env_test.sh b/tests/unit/kw_env_test.sh index 9f23b0ce3..901da34bf 100755 --- a/tests/unit/kw_env_test.sh +++ b/tests/unit/kw_env_test.sh @@ -44,19 +44,19 @@ function test_create_new_env_create_multiple_envs_from_current_configs() options_values['CREATE']='xpto' create_new_env - assertEquals "($LINENO) We should nota have errors" "$?" 0 + assertEquals "($LINENO) We should nota have errors" 0 "$?" options_values['CREATE']='abc' create_new_env - assertEquals "($LINENO) We should nota have errors" "$?" 0 + assertEquals "($LINENO) We should nota have errors" 0 "$?" # Other checks # 1. Do we have the env folder? new_env_name=$(find ".kw/${ENV_DIR}" -type d -name 'xpto') - assertEquals "($LINENO) We did not find the new folder name" "$new_env_name" ".kw/${ENV_DIR}/xpto" + assertEquals "($LINENO) We did not find the new folder name" ".kw/${ENV_DIR}/xpto" "$new_env_name" new_env_name=$(find ".kw/${ENV_DIR}" -type d -name 'abc') - assertEquals "($LINENO) We did not find the new folder name" "$new_env_name" ".kw/${ENV_DIR}/abc" + assertEquals "($LINENO) We did not find the new folder name" ".kw/${ENV_DIR}/abc" "$new_env_name" # 2. Check for config files for config in "${config_file_list[@]}"; do @@ -74,7 +74,7 @@ function test_create_new_env_outside_of_a_repo_without_init() options_values['CREATE']='farofa' output=$(create_new_env) - assertEquals "($LINENO) We should hit a fail condition" "$?" 22 + assertEquals "($LINENO) We should hit a fail condition" 22 "$?" } function test_create_new_env_missing_config() @@ -98,7 +98,7 @@ function test_create_new_env_check_if_target_env_name_already_exists() # Try to create the same env twice output=$(create_new_env) - assertEquals "($LINENO) We should be able to create two env with the same name" "$?" 22 + assertEquals "($LINENO) We should be able to create two env with the same name" 22 "$?" } function test_show_available_envs() @@ -129,7 +129,7 @@ function test_show_available_envs_when_we_dont_kw_folder() assertTrue "${LINENO}: Something went wrong when we tried to remove .kw folder" 'rm -rf .kw' output=$(list_env_available_envs) - assertEquals "($LINENO) We should hit a fail condition" "$?" 22 + assertEquals "($LINENO) We should hit a fail condition" 22 "$?" } function test_show_available_envs_when_there_is_no_env() @@ -166,7 +166,7 @@ function test_use_target_env() real_path=$(readlink "${PWD}/.kw/build.config") expected_path="${PWD}/.kw/${ENV_DIR}/farofa/build.config" - assertEquals "($LINENO) It looks like that the env did not switch" "$real_path" "$expected_path" + assertEquals "($LINENO) It looks like that the env did not switch" "$expected_path" "$real_path" # Switch env options_values['USE']='tapioca' @@ -175,7 +175,7 @@ function test_use_target_env() real_path=$(readlink "${PWD}/.kw/build.config") expected_path="${PWD}/.kw/${ENV_DIR}/tapioca/build.config" - assertEquals "($LINENO) It looks like that the env did not switch" "$real_path" "$expected_path" + assertEquals "($LINENO) It looks like that the env did not switch" "$expected_path" "$real_path" } function test_use_target_env_invalid_env() @@ -194,7 +194,7 @@ function test_use_target_env_invalid_env() # Switch env options_values['USE']='lala' output=$(use_target_env) - assertEquals "($LINENO) Env does not exists" "$?" 22 + assertEquals "($LINENO) Env does not exists" 22 "$?" } function test_validate_env_before_switch_invalid_case_with_config() @@ -242,11 +242,11 @@ function test_parse_env_options() # Check help output=$(parse_env_options -h) - assertEquals "($LINENO)" "$?" 0 + assertEquals "($LINENO)" 0 "$?" # Check verbose output=$(parse_env_options --verbose) - assertEquals "($LINENO)" "$?" 0 + assertEquals "($LINENO)" 0 "$?" # Check create parse_env_options --create abc @@ -254,10 +254,10 @@ function test_parse_env_options() "($LINENO)" 'abc' "${options_values['CREATE']}" output=$(parse_env_options --create 'abc la') - assertEquals "($LINENO) Invalid name" "$?" 22 + assertEquals "($LINENO) Invalid name" 22 "$?" output=$(parse_env_options --create 'Weird_n@m#') - assertEquals "($LINENO) Invalid name" "$?" 22 + assertEquals "($LINENO) Invalid name" 22 "$?" # Check use option parse_env_options --use abc @@ -266,10 +266,10 @@ function test_parse_env_options() # Check use option parse_env_options --an-invalid-option - assertEquals "($LINENO) Invalid option" "$?" 22 + assertEquals "($LINENO) Invalid option" 22 "$?" parse_env_options --use - assertEquals "($LINENO) Invalid option" "$?" 22 + assertEquals "($LINENO) Invalid option" 22 "$?" } function test_exit_env_checking_files() diff --git a/tests/unit/kw_remote_test.sh b/tests/unit/kw_remote_test.sh index b7088a27a..29a3bc173 100755 --- a/tests/unit/kw_remote_test.sh +++ b/tests/unit/kw_remote_test.sh @@ -46,15 +46,15 @@ function test_add_new_remote_wrong_number_of_parameters() options_values['PARAMETERS']='' output=$(add_new_remote) - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" options_values['PARAMETERS']='xpto' output=$(add_new_remote) - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" options_values['PARAMETERS']='xpto lala uuu' output=$(add_new_remote) - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" } function test_add_new_remote_no_kw_folder() @@ -67,7 +67,7 @@ function test_add_new_remote_no_kw_folder() options_values['PARAMETERS']='origin u' output=$(add_new_remote) - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" } function test_add_new_remote_with_no_config_file() @@ -213,11 +213,11 @@ function test_remove_remote_wrong_parameters() options_values['PARAMETERS']='' output=$(remove_remote) - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" options_values['PARAMETERS']='one two' output=$(remove_remote) - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" } function test_remove_remote_only_one_entry() @@ -235,7 +235,7 @@ function test_remove_remote_only_one_entry() options_values['PARAMETERS']='origin' output=$(remove_remote) mapfile -t final_result < "${BASE_PATH_KW}/remote.config" - assertEquals "($LINENO)" "${final_result[*]}" '' + assertEquals "($LINENO)" '' "${final_result[*]}" } function test_remove_remote_try_to_remove_something_from_an_empty_file() @@ -247,7 +247,7 @@ function test_remove_remote_try_to_remove_something_from_an_empty_file() options_values['PARAMETERS']='origin' output=$(remove_remote) mapfile -t final_result < "${BASE_PATH_KW}/remote.config" - assertEquals "($LINENO)" "${final_result[*]}" '' + assertEquals "($LINENO)" '' "${final_result[*]}" } function test_remove_remote_drop_guard_between_others() @@ -316,7 +316,7 @@ function test_remove_remote_try_to_drop_something_that_does_not_exists() # Remove a remote option in the middle options_values['PARAMETERS']='uva' output=$(remove_remote) - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" } function test_rename_remote_wrong_number_of_parameters() @@ -325,15 +325,15 @@ function test_rename_remote_wrong_number_of_parameters() options_values['PARAMETERS']='' output=$(rename_remote) - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" options_values['PARAMETERS']='xpto' output=$(rename_remote) - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" options_values['PARAMETERS']='xpto la lu' output=$(rename_remote) - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" } function test_rename_remote_try_to_rename_something_that_does_not_exists() @@ -344,7 +344,7 @@ function test_rename_remote_try_to_rename_something_that_does_not_exists() options_values['PARAMETERS']='ko uva' output=$(rename_remote) - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" } function test_rename_remote_rename_to_something_that_already_exists() @@ -355,7 +355,7 @@ function test_rename_remote_rename_to_something_that_already_exists() options_values['PARAMETERS']='fedora-test arch-test' output=$(rename_remote) - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" } function test_rename_remote_change_a_valid_remote() @@ -429,7 +429,7 @@ function test_set_default_remote_try_to_set_an_invalid_remote() options_values['DEFAULT_REMOTE']='palmares' output=$(set_default_remote) - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" } function test_set_default_remote_we_already_have_the_default_remote() @@ -468,18 +468,18 @@ function test_parse_remote_options() { # Add option parse_remote_options --add origin 'root@la:3333' - assert_equals_helper 'Request add' "($LINENO)" "${options_values['ADD']}" 1 - assert_equals_helper 'Remote options' "($LINENO)" "${options_values['PARAMETERS']}" 'origin root@la:3333 ' + assert_equals_helper 'Request add' "($LINENO)" 1 "${options_values['ADD']}" + assert_equals_helper 'Remote options' "($LINENO)" 'origin root@la:3333 ' "${options_values['PARAMETERS']}" # Remove parse_remote_options --remove origin - assert_equals_helper 'Request remove' "($LINENO)" "${options_values['REMOVE']}" 1 - assert_equals_helper 'Remote options' "($LINENO)" "${options_values['PARAMETERS']}" 'origin ' + assert_equals_helper 'Request remove' "($LINENO)" 1 "${options_values['REMOVE']}" + assert_equals_helper 'Remote options' "($LINENO)" 'origin ' "${options_values['PARAMETERS']}" # Rename parse_remote_options --rename origin xpto - assert_equals_helper 'Request rename' "($LINENO)" "${options_values['RENAME']}" 1 - assert_equals_helper 'Remote options' "($LINENO)" "${options_values['PARAMETERS']}" 'origin xpto ' + assert_equals_helper 'Request rename' "($LINENO)" 1 "${options_values['RENAME']}" + assert_equals_helper 'Remote options' "($LINENO)" 'origin xpto ' "${options_values['PARAMETERS']}" } function test_list_remotes() @@ -508,23 +508,23 @@ function test_list_remotes_invalid() { rm "${local_remote_config_file}" output=$(list_remotes) - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" rm -rf "${BASE_PATH_KW}" rm "${global_remote_config_file}" output=$(list_remotes) - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" } function test_kw_remote_without_valid_option() { # kw remote (no option nor parameter) parse_remote_options - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" # kw remote [...] (no options) parse_remote_options - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" } function test_remove_remote_that_is_prefix_of_other_remote() @@ -760,7 +760,7 @@ function test_global_option_list_remote_invalid() rm "${global_remote_config_file}" options_values['GLOBAL']='1' output=$(list_remotes) - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" } function setup_for_symbolic_link_test() diff --git a/tests/unit/kw_test.sh b/tests/unit/kw_test.sh index 462eab87f..8629f28fd 100755 --- a/tests/unit/kw_test.sh +++ b/tests/unit/kw_test.sh @@ -11,7 +11,7 @@ function test_validate_global_variables() VARS=(KWORKFLOW KW_LIB_DIR) for v in "${VARS[@]}"; do test -z ${!v+x} - assertEquals "Variable $v should exist." $? 1 + assertEquals "Variable ${v} should exist." 1 $? done } diff --git a/tests/unit/lib/dialog_ui_test.sh b/tests/unit/lib/dialog_ui_test.sh index 991dee926..00ee4b76c 100755 --- a/tests/unit/lib/dialog_ui_test.sh +++ b/tests/unit/lib/dialog_ui_test.sh @@ -107,7 +107,7 @@ function test_create_simple_checklist_rely_on_some_default_options() expected_cmd+=" $'Checklist 1' '' 'on' $'Checklist 2' '' 'off'" output=$(create_simple_checklist "$menu_title" "$menu_message_box" 'menu_list_string_array' 'check_statuses' '' '' '' '' '' 'TEST_MODE') - assert_equals_helper 'Expected simple checklist' "$LINENO" "${output}" "${expected_cmd}" + assert_equals_helper 'Expected simple checklist' "$LINENO" "${expected_cmd}" "${output}" } function test_create_simple_checklist_use_all_options() @@ -126,7 +126,7 @@ function test_create_simple_checklist_use_all_options() expected_cmd+=" $'Checklist 1' '' 'on' $'Checklist 2' '' 'off'" output=$(create_simple_checklist "$menu_title" "$menu_message_box" 'menu_list_string_array' 'check_statuses' 1 'Nop' '442' '244' '3' 'TEST_MODE') - assert_equals_helper 'Expected simple checklist' "$LINENO" "${output}" "${expected_cmd}" + assert_equals_helper 'Expected simple checklist' "$LINENO" "${expected_cmd}" "${output}" } function test_create_loading_screen_notification_rely_on_some_default_options() @@ -139,7 +139,7 @@ function test_create_loading_screen_notification_rely_on_some_default_options() expected_cmd+=" '8' '60'" output=$(create_loading_screen_notification "$loading_message" '' '' 'TEST_MODE') - assert_equals_helper 'Expected loading screen with some default options' "$LINENO" "${output}" "${expected_cmd}" + assert_equals_helper 'Expected loading screen with some default options' "$LINENO" "${expected_cmd}" "${output}" } function test_create_loading_screen_notification_use_all_options() @@ -152,7 +152,7 @@ function test_create_loading_screen_notification_use_all_options() expected_cmd+=" '1234' '4321'" output=$(create_loading_screen_notification "$loading_message" '1234' '4321' 'TEST_MODE') - assert_equals_helper 'Expected loading screen with some default options' "$LINENO" "${output}" "${expected_cmd}" + assert_equals_helper 'Expected loading screen with some default options' "$LINENO" "${expected_cmd}" "${output}" } function test_create_async_loading_screen_notification_rely_on_some_default_options() @@ -177,7 +177,7 @@ function test_create_async_loading_screen_notification_rely_on_some_default_opti stop_async_loading_screen_notification "$pid" output=$(< "$output_path") - assert_equals_helper 'Expected async loading screen with some default options' "$LINENO" "$output" "$expected_cmd" + assert_equals_helper 'Expected async loading screen with some default options' "$LINENO" "$expected_cmd" "$output" } function test_create_async_loading_screen_notification_use_all_options() @@ -202,7 +202,7 @@ function test_create_async_loading_screen_notification_use_all_options() stop_async_loading_screen_notification "$pid" output=$(< "$output_path") - assert_equals_helper 'Expected async loading screen with some default options' "$LINENO" "$output" "$expected_cmd" + assert_equals_helper 'Expected async loading screen with some default options' "$LINENO" "$expected_cmd" "$output" } function test_create_message_box_rely_on_some_default_options() @@ -216,7 +216,7 @@ function test_create_message_box_rely_on_some_default_options() expected_cmd+=" --msgbox $'There\'re no bookmarked patches...'" expected_cmd+=" '15' '40'" output=$(create_message_box "${box_title}" "${message_box}" '' '' 'TEST_MODE') - assert_equals_helper 'Expected message box with some default options' "$LINENO" "$output" "${expected_cmd}" + assert_equals_helper 'Expected message box with some default options' "$LINENO" "${expected_cmd}" "$output" } function test_create_message_box_use_all_options() @@ -230,7 +230,7 @@ function test_create_message_box_use_all_options() expected_cmd+=" --msgbox $'There\'re no bookmarked patches...'" expected_cmd+=" '1234' '4321'" output=$(create_message_box "${box_title}" "${message_box}" '1234' '4321' 'TEST_MODE') - assert_equals_helper 'Expected message box with all custom options' "$LINENO" "$output" "${expected_cmd}" + assert_equals_helper 'Expected message box with all custom options' "$LINENO" "${expected_cmd}" "$output" } function test_create_directory_selection_screen_rely_on_some_default_options() @@ -569,10 +569,10 @@ function test_build_dialog_command_preamble() function test_prettify_string_failures() { prettify_string - assert_equals_helper 'Expected failure' "$LINENO" "$?" 22 + assert_equals_helper 'Expected failure' "$LINENO" 22 "$?" prettify_string 'Something' - assert_equals_helper 'Expected failure' "$LINENO" "$?" 22 + assert_equals_helper 'Expected failure' "$LINENO" 22 "$?" } function test_prettify_string() @@ -581,7 +581,7 @@ function test_prettify_string() local expected_string='\Zb\Z6Series:\ZnSomething\n' output=$(prettify_string 'Series:' 'Something') - assert_equals_helper 'Expected pretty string' "$LINENO" "$output" "$expected_string" + assert_equals_helper 'Expected pretty string' "$LINENO" "$expected_string" "$output" } invoke_shunit diff --git a/tests/unit/lib/kw_config_loader_test.sh b/tests/unit/lib/kw_config_loader_test.sh index 9db669a71..13d21b6e3 100755 --- a/tests/unit/lib/kw_config_loader_test.sh +++ b/tests/unit/lib/kw_config_loader_test.sh @@ -73,16 +73,16 @@ function test_parse_configuration_config_with_spaces_and_comments() parse_configuration 'kworkflow_space_comments.config' assertEquals "($LINENO): Kw failed to load a regular config file" 0 "$?" - assertEquals "($LINENO)" "${configurations['ssh_user']}" 'juca' - assertEquals "($LINENO)" "${configurations['mount_point']}" '/home/lala' - assertEquals "($LINENO)" "${configurations['virtualizer']}" 'libvirt' - assertEquals "($LINENO)" "${configurations['reboot_after_deploy']}" 'no' + assertEquals "($LINENO)" 'juca' "${configurations['ssh_user']}" + assertEquals "($LINENO)" '/home/lala' "${configurations['mount_point']}" + assertEquals "($LINENO)" 'libvirt' "${configurations['virtualizer']}" + assertEquals "($LINENO)" 'no' "${configurations['reboot_after_deploy']}" } function test_parser_configuration_failed_exit_code() { parse_configuration 'tests/unit/foobarpotato' - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" } # Helper function used to compare expected config agaist the populated data. @@ -119,6 +119,8 @@ function test_parse_configuration_check_parser_values_only_for_kworkflow_config_ [ssh_port]='3333' [gui_on]='turn on' [gui_off]='turn off' + [gui_on_after_reboot]='turn on after reboot' + [gui_off_after_reboot]='turn off after reboot' [checkpatch_opts]='--no-tree --color=always --strict' [get_maintainer_opts]='--separator , --nokeywords --nogit --nogit-fallback --norolestats' ) @@ -270,13 +272,13 @@ function test_load_configuration() # No to updating kworkflow.config to .kw/kworkflow.config output="$(printf '%s\n' 'n' | load_kworkflow_config)" - assertEquals "($LINENO): There should have been a warning" "$output" "$msg" + assertEquals "($LINENO): There should have been a warning" "$msg" "$output" assertTrue 'kworkflow.config was moved' '[[ -f "$PWD/$CONFIG_FILENAME" ]]' # Yes to updating kworkflow.config to .kw/kworkflow.config output="$(printf '%s\n' 'y' | load_configuration)" - assertEquals "($LINENO): There should have been a warning" "$output" "$msg" + assertEquals "($LINENO): There should have been a warning" "$msg" "$output" assertTrue '.kw was not created' '[[ -d "$PWD/$KW_DIR/" ]]' assertTrue 'kworkflow.config is not inside .kw' '[[ -f "$PWD/$KW_DIR/$CONFIG_FILENAME" ]]' diff --git a/tests/unit/lib/kw_include_test.sh b/tests/unit/lib/kw_include_test.sh index 41cff4565..c191db80e 100755 --- a/tests/unit/lib/kw_include_test.sh +++ b/tests/unit/lib/kw_include_test.sh @@ -16,13 +16,13 @@ function oneTimeSetUp() touch "${test_files[@]}" # the next files will be checked for name collisions - printf "%s\n" "function test1(){ printf '%s\n' 'output of test1';}" > \ - "$SHUNIT_TMPDIR/include_test_similar_path.sh" + printf "%s\n" "function test1(){ printf '%s\n' 'output of test1';}" > "\ +$SHUNIT_TMPDIR/include_test_similar_path.sh" mkdir "$SHUNIT_TMPDIR/include_test" - printf "%s\n" "function test2(){ printf '%s\n' 'output of test2';}" > \ - "$SHUNIT_TMPDIR/include_test/similar_path.sh" + printf "%s\n" "function test2(){ printf '%s\n' 'output of test2';}" > "\ +$SHUNIT_TMPDIR/include_test/similar_path.sh" } function oneTimeTearDown() @@ -80,8 +80,8 @@ function test_include_similar_paths() test1_expected='output of test1' test2_expected='output of test2' - assertEquals "($LINENO)" "$test1_output" "$test1_expected" - assertEquals "($LINENO)" "$test2_output" "$test2_expected" + assertEquals "($LINENO)" "$test1_expected" "$test1_output" + assertEquals "($LINENO)" "$test2_expected" "$test2_output" } invoke_shunit diff --git a/tests/unit/lib/kw_string_test.sh b/tests/unit/lib/kw_string_test.sh index 8640b0c20..6462f9521 100755 --- a/tests/unit/lib/kw_string_test.sh +++ b/tests/unit/lib/kw_string_test.sh @@ -294,20 +294,20 @@ function test_concatenate_with_commas() output=$(concatenate_with_commas) ret="$?" expected='' - assert_equals_helper 'No error expected' "$LINENO" "$ret" 0 - assert_equals_helper 'Expected empty string' "$LINENO" "$output" "$expected" + assert_equals_helper 'No error expected' "$LINENO" 0 "$ret" + assert_equals_helper 'Expected empty string' "$LINENO" "$expected" "$output" output=$(concatenate_with_commas 'single') ret="$?" expected='single' - assert_equals_helper 'No error expected' "$LINENO" "$ret" 0 - assert_equals_helper 'Wrong output' "$LINENO" "$output" "$expected" + assert_equals_helper 'No error expected' "$LINENO" 0 "$ret" + assert_equals_helper 'Wrong output' "$LINENO" "$expected" "$output" output=$(concatenate_with_commas 'first' 'second' 'third') ret="$?" expected='first,second,third' - assert_equals_helper 'No error expected' "$LINENO" "$ret" 0 - assert_equals_helper 'Wrong output' "$LINENO" "$output" "$expected" + assert_equals_helper 'No error expected' "$LINENO" 0 "$ret" + assert_equals_helper 'Wrong output' "$LINENO" "$expected" "$output" } function test_str_has_special_characters() @@ -317,10 +317,10 @@ function test_str_has_special_characters() local ret output=$(str_has_special_characters 'no special char here') - assert_equals_helper 'No error expected' "$LINENO" "$?" 1 + assert_equals_helper 'No error expected' "$LINENO" 1 "$?" output=$(str_has_special_characters 'We have a special char!') - assert_equals_helper 'We expected a special char here' "$LINENO" "$?" 0 + assert_equals_helper 'We expected a special char here' "$LINENO" 0 "$?" } function test_str_get_value_under_double_quotes() @@ -329,18 +329,18 @@ function test_str_get_value_under_double_quotes() local expected='value under quotes' output=$(str_get_value_under_double_quotes 'This is a "value under quotes", right?') - assert_equals_helper 'Wrong values under quotes' "$LINENO" "$output" "$expected" + assert_equals_helper 'Wrong values under quotes' "$LINENO" "$expected" "$output" expected='Nothing around quotes' output=$(str_get_value_under_double_quotes '"Nothing around quotes"') - assert_equals_helper 'Wrong values under quotes' "$LINENO" "$output" "$expected" + assert_equals_helper 'Wrong values under quotes' "$LINENO" "$expected" "$output" expected='Two' output=$(str_get_value_under_double_quotes '"Two" and "Nothing around quotes" and "xpto"') - assert_equals_helper 'Wrong values under quotes' "$LINENO" "$output" "$expected" + assert_equals_helper 'Wrong values under quotes' "$LINENO" "$expected" "$output" output=$(str_get_value_under_double_quotes '') - assert_equals_helper 'Empty string' "$LINENO" "$?" 22 + assert_equals_helper 'Empty string' "$LINENO" 22 "$?" } diff --git a/tests/unit/lib/kw_time_and_date_test.sh b/tests/unit/lib/kw_time_and_date_test.sh index 5ea88e318..42c87b9c2 100755 --- a/tests/unit/lib/kw_time_and_date_test.sh +++ b/tests/unit/lib/kw_time_and_date_test.sh @@ -13,16 +13,16 @@ function setUp() function test_sec_to_format() { formatted_time=$(sec_to_format "$pre_total_sec") - assertEquals "($LINENO)" "$formatted_time" "$pre_formated_sec" + assertEquals "($LINENO)" "$pre_formated_sec" "$formatted_time" formatted_time=$(sec_to_format "") - assertEquals "($LINENO)" "$formatted_time" '00:00:00' + assertEquals "($LINENO)" '00:00:00' "$formatted_time" formatted_time=$(sec_to_format "$pre_total_sec" '+%M:%S') - assertEquals "($LINENO)" "$formatted_time" '30:46' + assertEquals "($LINENO)" '30:46' "$formatted_time" formatted_time=$(sec_to_format "$pre_total_sec" '+%S') - assertEquals "($LINENO)" "$formatted_time" '46' + assertEquals "($LINENO)" '46' "$formatted_time" } function test_secs_to_arbitrarily_long_hours_mins_secs() @@ -152,14 +152,14 @@ function test_date_to_format() local formatted_date formatted_date=$(date_to_format '2020/3/1') - assert_equals_helper 'Today' "$LINENO" "$formatted_date" '2020/03/01' + assert_equals_helper 'Today' "$LINENO" '2020/03/01' "$formatted_date" formatted_date=$(date_to_format '2020/3/1' '+%Y/%m') - assert_equals_helper 'Today' "$LINENO" "$formatted_date" '2020/03' + assert_equals_helper 'Today' "$LINENO" '2020/03' "$formatted_date" formatted_date=$(date_to_format) today=$(date '+%Y/%m/%d') - assert_equals_helper 'Today' "$LINENO" "$formatted_date" "$today" + assert_equals_helper 'Today' "$LINENO" "$today" "$formatted_date" } function test_days_in_the_month() @@ -171,57 +171,57 @@ function test_days_in_the_month() local ret total_days=$(days_in_the_month 2 2021) - assert_equals_helper 'We expect 28 days' "$LINENO" "$total_days" 28 + assert_equals_helper 'We expect 28 days' "$LINENO" 28 "$total_days" total_days=$(days_in_the_month 02 2021) - assert_equals_helper 'We expect 28 days' "$LINENO" "$total_days" 28 + assert_equals_helper 'We expect 28 days' "$LINENO" 28 "$total_days" # Leap year, February has 29 days total_days=$(days_in_the_month 2 2016) - assert_equals_helper 'We expect 29 days' "$LINENO" "$total_days" 29 + assert_equals_helper 'We expect 29 days' "$LINENO" 29 "$total_days" total_days=$(days_in_the_month 2 300) - assert_equals_helper 'We expect 28 days' "$LINENO" "$total_days" 28 + assert_equals_helper 'We expect 28 days' "$LINENO" 28 "$total_days" # Leap year, February has 29 days total_days=$(days_in_the_month 2 1600) - assert_equals_helper 'We expect 29 days' "$LINENO" "$total_days" 29 + assert_equals_helper 'We expect 29 days' "$LINENO" 29 "$total_days" total_days=$(days_in_the_month 1 2016) - assert_equals_helper 'We expect 31 days' "$LINENO" "$total_days" 31 + assert_equals_helper 'We expect 31 days' "$LINENO" 31 "$total_days" total_days=$(days_in_the_month 6 2021) - assert_equals_helper 'We expect 30 days' "$LINENO" "$total_days" 30 + assert_equals_helper 'We expect 30 days' "$LINENO" 30 "$total_days" total_days=$(days_in_the_month 9 2021) - assert_equals_helper 'We expect 30 days' "$LINENO" "$total_days" 30 + assert_equals_helper 'We expect 30 days' "$LINENO" 30 "$total_days" total_days=$(days_in_the_month 09 2021) - assert_equals_helper 'We expect 30 days' "$LINENO" "$total_days" 30 + assert_equals_helper 'We expect 30 days' "$LINENO" 30 "$total_days" total_days=$(days_in_the_month 8 2021) - assert_equals_helper 'We expect 31 days' "$LINENO" "$total_days" 31 + assert_equals_helper 'We expect 31 days' "$LINENO" 31 "$total_days" # Empty year should be converted to the present year total_days=$(days_in_the_month 8) - assert_equals_helper 'Use this year' "$LINENO" "$total_days" 31 + assert_equals_helper 'Use this year' "$LINENO" 31 "$total_days" # An invalid month days_in_the_month 333 ret="$?" - assert_equals_helper 'Invalid month' "$LINENO" "$ret" 22 + assert_equals_helper 'Invalid month' "$LINENO" 22 "$ret" days_in_the_month -5 ret="$?" - assert_equals_helper 'Invalid month' "$LINENO" "$ret" 22 + assert_equals_helper 'Invalid month' "$LINENO" 22 "$ret" days_in_the_month -09 ret="$?" - assert_equals_helper 'Invalid month' "$LINENO" "$ret" 22 + assert_equals_helper 'Invalid month' "$LINENO" 22 "$ret" days_in_the_month -009 ret="$?" - assert_equals_helper 'Invalid month' "$LINENO" "$ret" 22 + assert_equals_helper 'Invalid month' "$LINENO" 22 "$ret" } function test_timebox_to_sec() diff --git a/tests/unit/lib/kwlib_test.sh b/tests/unit/lib/kwlib_test.sh index f0e3bcf08..4632817de 100755 --- a/tests/unit/lib/kwlib_test.sh +++ b/tests/unit/lib/kwlib_test.sh @@ -230,10 +230,10 @@ function test_cmd_manager_check_test_mode_option() local ret ret=$(cmd_manager 'TEST_MODE' 'pwd') - assertEquals "Expected pwd, but we got $ret" "$ret" "pwd" + assertEquals "Expected pwd, but we got ${ret}" "pwd" "$ret" ret=$(cmd_manager 'TEST_MODE' 'ls -lah') - assertEquals "Expected ls -lah, but we got $ret" "$ret" "ls -lah" + assertEquals "Expected ls -lah, but we got ${ret}" "ls -lah" "$ret" } function test_detect_distro_root_path_only() @@ -243,47 +243,47 @@ function test_detect_distro_root_path_only() root_path="${SAMPLES_DIR}/os/arch" output=$(detect_distro "$root_path") - assert_equals_helper '' "$LINENO" "$output" 'arch' + assert_equals_helper '' "$LINENO" 'arch' "$output" root_path="${SAMPLES_DIR}/os/manjaro" output=$(detect_distro "$root_path") - assert_equals_helper '' "$LINENO" "$output" 'arch' + assert_equals_helper '' "$LINENO" 'arch' "$output" root_path="${SAMPLES_DIR}/os/ubuntu" output=$(detect_distro "$root_path") - assert_equals_helper '' "$LINENO" "$output" 'debian' + assert_equals_helper '' "$LINENO" 'debian' "$output" root_path="${SAMPLES_DIR}/os/debian" output=$(detect_distro "$root_path") - assert_equals_helper '' "$LINENO" "$output" 'debian' + assert_equals_helper '' "$LINENO" 'debian' "$output" root_path="${SAMPLES_DIR}/os/raspbian" output=$(detect_distro "$root_path") - assert_equals_helper '' "$LINENO" "$output" 'debian' + assert_equals_helper '' "$LINENO" 'debian' "$output" root_path="${SAMPLES_DIR}/os/fedora" output=$(detect_distro "$root_path") - assert_equals_helper '' "$LINENO" "$output" 'fedora' + assert_equals_helper '' "$LINENO" 'fedora' "$output" root_path="${SAMPLES_DIR}/os/arch-linux-arm" output=$(detect_distro "$root_path") - assert_equals_helper '' "$LINENO" "$output" 'arch' + assert_equals_helper '' "$LINENO" 'arch' "$output" root_path="${SAMPLES_DIR}/os/endeavouros" output=$(detect_distro "$root_path") - assert_equals_helper '' "$LINENO" "$output" 'arch' + assert_equals_helper '' "$LINENO" 'arch' "$output" root_path="${SAMPLES_DIR}/os/steamos" output=$(detect_distro "$root_path") - assert_equals_helper '' "$LINENO" "$output" 'arch' + assert_equals_helper '' "$LINENO" 'arch' "$output" root_path="${SAMPLES_DIR}/os/popos" output=$(detect_distro "$root_path") - assert_equals_helper '' "$LINENO" "$output" 'debian' + assert_equals_helper '' "$LINENO" 'debian' "$output" root_path="${SAMPLES_DIR}/os/none" output=$(detect_distro "$root_path") - assert_equals_helper '' "$LINENO" "$output" 'none' + assert_equals_helper '' "$LINENO" 'none' "$output" } function test_detect_distro_str_check() @@ -292,22 +292,22 @@ function test_detect_distro_str_check() local output output=$(detect_distro '/' 'arch') - assert_equals_helper '' "$LINENO" "$output" 'arch' + assert_equals_helper '' "$LINENO" 'arch' "$output" output=$(detect_distro '' 'debian') - assert_equals_helper '' "$LINENO" "$output" 'debian' + assert_equals_helper '' "$LINENO" 'debian' "$output" output=$(detect_distro '' 'fedora') - assert_equals_helper '' "$LINENO" "$output" 'fedora' + assert_equals_helper '' "$LINENO" 'fedora' "$output" output=$(detect_distro '' 'ubuntu') - assert_equals_helper '' "$LINENO" "$output" 'none' + assert_equals_helper '' "$LINENO" 'none' "$output" output=$(detect_distro '' 'ubuntu debian') - assert_equals_helper '' "$LINENO" "$output" 'debian' + assert_equals_helper '' "$LINENO" 'debian' "$output" output=$(detect_distro '' 'manjaro steamos lala arch') - assert_equals_helper '' "$LINENO" "$output" 'arch' + assert_equals_helper '' "$LINENO" 'arch' "$output" } function test_detect_distro_from_raw_data() @@ -319,57 +319,57 @@ function test_detect_distro_from_raw_data() root_path="${SAMPLES_DIR}/os/arch/etc/os-release" os_release_data=$(< "$root_path") output=$(detect_distro '' '' "$os_release_data") - assert_equals_helper '' "$LINENO" "$output" 'arch' + assert_equals_helper '' "$LINENO" 'arch' "$output" root_path="${SAMPLES_DIR}/os/manjaro/etc/os-release" os_release_data=$(< "$root_path") output=$(detect_distro '' '' "$os_release_data") - assert_equals_helper '' "$LINENO" "$output" 'arch' + assert_equals_helper '' "$LINENO" 'arch' "$output" root_path="${SAMPLES_DIR}/os/ubuntu/etc/os-release" os_release_data=$(< "$root_path") output=$(detect_distro '' '' "$os_release_data") - assert_equals_helper '' "$LINENO" "$output" 'debian' + assert_equals_helper '' "$LINENO" 'debian' "$output" root_path="${SAMPLES_DIR}/os/debian/etc/os-release" os_release_data=$(< "$root_path") output=$(detect_distro '' '' "$os_release_data") - assert_equals_helper '' "$LINENO" "$output" 'debian' + assert_equals_helper '' "$LINENO" 'debian' "$output" root_path="${SAMPLES_DIR}/os/raspbian/etc/os-release" os_release_data=$(< "$root_path") output=$(detect_distro '' '' "$os_release_data") - assert_equals_helper '' "$LINENO" "$output" 'debian' + assert_equals_helper '' "$LINENO" 'debian' "$output" root_path="${SAMPLES_DIR}/os/fedora/etc/os-release" os_release_data=$(< "$root_path") output=$(detect_distro '' '' "$os_release_data") - assert_equals_helper '' "$LINENO" "$output" 'fedora' + assert_equals_helper '' "$LINENO" 'fedora' "$output" root_path="${SAMPLES_DIR}/os/arch-linux-arm/etc/os-release" os_release_data=$(< "$root_path") output=$(detect_distro '' '' "$os_release_data") - assert_equals_helper '' "$LINENO" "$output" 'arch' + assert_equals_helper '' "$LINENO" 'arch' "$output" root_path="${SAMPLES_DIR}/os/endeavouros/etc/os-release" os_release_data=$(< "$root_path") output=$(detect_distro '' '' "$os_release_data") - assert_equals_helper '' "$LINENO" "$output" 'arch' + assert_equals_helper '' "$LINENO" 'arch' "$output" root_path="${SAMPLES_DIR}/os/steamos/etc/os-release" os_release_data=$(< "$root_path") output=$(detect_distro '' '' "$os_release_data") - assert_equals_helper '' "$LINENO" "$output" 'arch' + assert_equals_helper '' "$LINENO" 'arch' "$output" root_path="${SAMPLES_DIR}/os/popos/etc/os-release" os_release_data=$(< "$root_path") output=$(detect_distro '' '' "$os_release_data") - assert_equals_helper '' "$LINENO" "$output" 'debian' + assert_equals_helper '' "$LINENO" 'debian' "$output" root_path="${SAMPLES_DIR}/os/none/etc/os-release" os_release_data=$(< "$root_path") output=$(detect_distro '' '' "$os_release_data") - assert_equals_helper '' "$LINENO" "$output" 'none' + assert_equals_helper '' "$LINENO" 'none' "$output" } function test_join_path() @@ -378,19 +378,19 @@ function test_join_path() local ret ret=$(join_path "/lala" "///xpto") - assertEquals "Expect /lala/xpto" "$ret" "$base" + assertEquals "Expect /lala/xpto" "$base" "$ret" ret=$(join_path "/lala" "/xpto////") - assertEquals "Expect /lala/xpto" "$ret" "$base" + assertEquals "Expect /lala/xpto" "$base" "$ret" ret=$(join_path "/lala" "////xpto////") - assertEquals "Expect /lala/xpto" "$ret" "$base" + assertEquals "Expect /lala/xpto" "$base" "$ret" ret=$(join_path "/lala" "//test///xpto////") - assertEquals "Expect /lala/test/xpto" "$ret" "/lala/test/xpto" + assertEquals "Expect /lala/test/xpto" "/lala/test/xpto" "$ret" ret=$(join_path "/lala/") - assertEquals "Expect /lala/" "$ret" "/lala/" + assertEquals "Expect /lala/" "/lala/" "$ret" } function test_find_kernel_root() @@ -401,13 +401,13 @@ function test_find_kernel_root() mkdir -p "$fake_path" kernel_path=$(find_kernel_root "$fake_path") - assertEquals "We expected to find a kernel path" "$kernel_path" "$SHUNIT_TMPDIR" + assertEquals "We expected to find a kernel path" "$SHUNIT_TMPDIR" "$kernel_path" kernel_path=$(find_kernel_root "/tmp") - assertEquals "We should not find a path" "$kernel_path" "" + assertEquals "We should not find a path" "" "$kernel_path" kernel_path=$(find_kernel_root "test/") - assertEquals "We should not find a path" "$kernel_path" "" + assertEquals "We should not find a path" "" "$kernel_path" } function test_is_a_patch() diff --git a/tests/unit/lib/lore_test.sh b/tests/unit/lib/lore_test.sh index 1725a9eb7..9ff11340c 100755 --- a/tests/unit/lib/lore_test.sh +++ b/tests/unit/lib/lore_test.sh @@ -66,12 +66,12 @@ function test_retrieve_available_mailing_lists() for index in "${!expected_lists[@]}"; do assert_equals_helper "We expected '$index' to be a valid key" "($LINENO)" \ - "${available_lore_mailing_lists["$index"]}" "${expected_lists["$index"]}" + "${expected_lists["$index"]}" "${available_lore_mailing_lists["$index"]}" done for index in "${!available_lore_mailing_lists[@]}"; do assert_equals_helper "We didn't expect '$index' to be a valid key" "($LINENO)" \ - "${available_lore_mailing_lists["$index"]}" "${expected_lists["$index"]}" + "${expected_lists["$index"]}" "${available_lore_mailing_lists["$index"]}" done } @@ -406,15 +406,15 @@ function test_process_name() local expected='First Second' output=$(process_name 'Second, First') - assertEquals "($LINENO)" "$output" "$expected" + assertEquals "($LINENO)" "$expected" "$output" output=$(process_name 'Second Third, First') expected='First Second Third' - assertEquals "($LINENO)" "$output" "$expected" + assertEquals "($LINENO)" "$expected" "$output" output=$(process_name 'First Second') expected='First Second' - assertEquals "($LINENO)" "$output" "$expected" + assertEquals "($LINENO)" "$expected" "$output" } function test_delete_series_from_local_storage() diff --git a/tests/unit/lib/remote_test.sh b/tests/unit/lib/remote_test.sh index e89d113ee..edd5cd2fd 100755 --- a/tests/unit/lib/remote_test.sh +++ b/tests/unit/lib/remote_test.sh @@ -328,7 +328,7 @@ function test_cmd_remote() remote_parameters['REMOTE_FILE_HOST']='origin' expected_command="ssh -F ${SHUNIT_TMPDIR}/remote.config origin sudo \"$command\"" - output=$(cmd_remotely "$command" "$flag") + output=$(cmd_remotely "$flag" "$command") assertEquals "($LINENO): Command did not match" "$expected_command" "$output" configurations=() @@ -340,23 +340,23 @@ function test_cmd_remote() remote_parameters['REMOTE_USER']='root' expected_command="ssh -p $port $user@$remote sudo \"$command\"" - output=$(cmd_remotely "$command" "$flag" "$remote" "$port" "$user") + output=$(cmd_remotely "$flag" "$command" "$remote" "$port" "$user") assertEquals "($LINENO):" "$expected_command" "$output" expected_command="ssh -p $port $user@localhost sudo \"$command\"" - output=$(cmd_remotely "$command" "$flag" '' "$port" "$user") + output=$(cmd_remotely "$flag" "$command" '' "$port" "$user") assertEquals "($LINENO):" "$expected_command" "$output" expected_command="ssh -p 22 $user@localhost sudo \"$command\"" - output=$(cmd_remotely "$command" "$flag" '' '' "$user") + output=$(cmd_remotely "$flag" "$command" '' '' "$user") assertEquals "($LINENO):" "$expected_command" "$output" expected_command="ssh -p 22 root@localhost sudo \"$command\"" - output=$(cmd_remotely "$command" "$flag") + output=$(cmd_remotely "$flag" "$command") assertEquals "($LINENO):" "$expected_command" "$output" expected_command="No command specified" - output=$(cmd_remotely '' "$flag") + output=$(cmd_remotely "$flag" '') assertEquals "($LINENO):" "$expected_command" "$output" } diff --git a/tests/unit/lib/statistics_test.sh b/tests/unit/lib/statistics_test.sh index 3a483f7a6..dc15c2c55 100755 --- a/tests/unit/lib/statistics_test.sh +++ b/tests/unit/lib/statistics_test.sh @@ -64,10 +64,10 @@ function test_calculate_total_of_data() function test_max_value() { max=$(max_value "0") - assertEquals "($LINENO)" "$max" "0" + assertEquals "($LINENO)" 0 "$max" max=$(max_value "") - assertEquals "($LINENO)" "$max" "0" + assertEquals "($LINENO)" 0 "$max" max=$(max_value "$pre_values") assertEquals "($LINENO)" "$pre_max" "$max" @@ -76,13 +76,13 @@ function test_max_value() function test_min_value() { min=$(min_value "0" "0") - assertEquals "($LINENO)" "$min" "0" + assertEquals "($LINENO)" 0 "$min" min=$(min_value "" "") - assertEquals "($LINENO)" "$min" "" + assertEquals "($LINENO)" "" "$min" min=$(min_value "$pre_values" "$pre_max") - assertEquals "($LINENO)" "$min" "$pre_min" + assertEquals "($LINENO)" "$pre_min" "$min" } # Note: The weekly, monthly, and yearly calculation uses `basic_data_process`. @@ -98,11 +98,11 @@ function test_basic_data_process() basic_data_process "$data" build="${shared_data["build"]}" - assertEquals "($LINENO)" "$build_output" "$build" + assertEquals "($LINENO)" "$build" "$build_output" basic_data_process "$data" deploy="${shared_data["deploy"]}" - assertEquals "($LINENO)" "$deploy_output" "$deploy" + assertEquals "($LINENO)" "$deploy" "$deploy_output" basic_data_process "$data" deploy="${shared_data["list"]}" diff --git a/tests/unit/lib/web_test.sh b/tests/unit/lib/web_test.sh index ba74de608..ceed94efe 100755 --- a/tests/unit/lib/web_test.sh +++ b/tests/unit/lib/web_test.sh @@ -37,20 +37,20 @@ function test_download() output=$(download '' '' '' 'TEST_MODE') ret="$?" expected='URL must not be empty.' - assert_equals_helper 'We expected to get an error for an empty URL' "($LINENO)" "$ret" 22 - assert_equals_helper 'We expected an error message' "($LINENO)" "$output" "$expected" + assert_equals_helper 'We expected to get an error for an empty URL' "($LINENO)" 22 "$ret" + assert_equals_helper 'We expected an error message' "($LINENO)" "$expected" "$output" output=$(download 'http://some-url.com' '' '' 'TEST_MODE') expected="curl --silent 'http://some-url.com' --output 'fake_cache/page.xml'" - assert_equals_helper 'We expected the correct curl command' "($LINENO)" "$output" "$expected" + assert_equals_helper 'We expected the correct curl command' "($LINENO)" "$expected" "$output" output=$(download 'http://some-url.com' 'some-file.html' '' 'TEST_MODE') expected="curl --silent 'http://some-url.com' --output 'fake_cache/some-file.html'" - assert_equals_helper 'We expected a custom file name' "($LINENO)" "$output" "$expected" + assert_equals_helper 'We expected a custom file name' "($LINENO)" "$expected" "$output" output=$(download 'http://some-url.com' 'some-file.html' 'alt_path' 'TEST_MODE') expected="curl --silent 'http://some-url.com' --output 'alt_path/some-file.html'" - assert_equals_helper 'We expected a custom file name and path' "($LINENO)" "$output" "$expected" + assert_equals_helper 'We expected a custom file name and path' "($LINENO)" "$expected" "$output" } function test_replace_http_by_https() @@ -59,15 +59,15 @@ function test_replace_http_by_https() local expected='https://lore.kernel.org/' output=$(replace_http_by_https 'http://lore.kernel.org/') - assert_equals_helper 'Expected https' "($LINENO)" "$output" "$expected" + assert_equals_helper 'Expected https' "($LINENO)" "$expected" "$output" output=$(replace_http_by_https 'https://lore.kernel.org/') - assert_equals_helper 'Expected https' "($LINENO)" "$output" "$expected" + assert_equals_helper 'Expected https' "($LINENO)" "$expected" "$output" expected='lore.kernel.org/' output=$(replace_http_by_https 'lore.kernel.org/') - assert_equals_helper 'No http prefix' "($LINENO)" "$?" 1 - assert_equals_helper 'Expected https' "($LINENO)" "$output" "$expected" + assert_equals_helper 'No http prefix' "($LINENO)" 1 "$?" + assert_equals_helper 'Expected https' "($LINENO)" "$expected" "$output" } function test_is_html_file_with_non_html_files() diff --git a/tests/unit/mail_test.sh b/tests/unit/mail_test.sh index a65f548fc..e16563d79 100755 --- a/tests/unit/mail_test.sh +++ b/tests/unit/mail_test.sh @@ -963,7 +963,7 @@ function test_mail_verify() output=$(mail_verify) ret="$?" - assert_equals_helper 'Failed verify expected an error' "$LINENO" "$ret" 22 + assert_equals_helper 'Failed verify expected an error' "$LINENO" 22 "$ret" compare_command_sequence '' "$LINENO" 'expected_results' "$output" unset options_values @@ -988,7 +988,7 @@ function test_mail_verify() output=$(mail_verify) ret="$?" - assert_equals_helper 'Expected a success' "$LINENO" "$ret" 0 + assert_equals_helper 'Expected a success' "$LINENO" 0 "$ret" compare_command_sequence '' "$LINENO" 'expected_results' "$output" unset options_values diff --git a/tests/unit/maintainers_test.sh b/tests/unit/maintainers_test.sh index 0f04c8427..80a9bfc13 100755 --- a/tests/unit/maintainers_test.sh +++ b/tests/unit/maintainers_test.sh @@ -11,7 +11,8 @@ include './tests/unit/utils.sh' CORRECT_DIR_MSG="========================================================= MODULE AUTHORS: code1.c: John Doe , Caesar Doe , Michael Doe -code2.c: Bob Hilson " +code2.c: Bob Hilson +multiline_authors_test.c: Martha Elsa , Verena Bert , Gabi Katinka , Leonie Hildebert " CORRECT_FILE_MSG="========================================================= MODULE AUTHORS: code1.c: John Doe , Caesar Doe , Michael Doe " @@ -43,6 +44,9 @@ Maintainers already in 'To:' field of update_patch_test.patch" FAKE_KERNEL="tests/.tmp" +# Original directory path to go back before each test function +ORIGINAL_DIR="$PWD" + function oneTimeSetUp() { # This creates tests/.tmp which should mock a kernel tree root. A .git @@ -54,8 +58,9 @@ function oneTimeSetUp() cp -f tests/unit/samples/MAINTAINERS "$FAKE_KERNEL"/MAINTAINERS cp -f tests/unit/samples/external/get_maintainer.pl "$FAKE_KERNEL"/scripts/ cp -f tests/unit/samples/update_patch_test{_model,}{,2}.patch "$FAKE_KERNEL"/ + cp --force 'tests/unit/samples/cover_letter_test.patch' "$FAKE_KERNEL" cd "$FAKE_KERNEL" || { - fail "($LINENO) It was not possible to move to temporary directory" + fail "(${LINENO}) It was not possible to move to temporary directory" return } touch fs/some_file @@ -65,7 +70,7 @@ function oneTimeSetUp() git config user.email kw@kw git commit --quiet -m "Test message" cd "$original_dir" || { - fail "($LINENO) It was not possible to move back from temp directory" + fail "(${LINENO}) It was not possible to move back from temp directory" return } @@ -77,88 +82,223 @@ function oneTimeTearDown() rm -rf "$FAKE_KERNEL" } +function setUp() +{ + # Ensure each test function starts running in ORIGINAL_DIR. + cd "$ORIGINAL_DIR" || { + fail "(${LINENO}) It was not possible to move to original dir" + return + } +} + +function tearDown() +{ + # Check if SHUNIT_TMPDIR can be safely removed and then remove it and remake it. + is_safe_path_to_remove "$SHUNIT_TMPDIR" + if [[ "$?" == 0 ]]; then + rm --recursive --force "$SHUNIT_TMPDIR" + mkdir --parents "$SHUNIT_TMPDIR" + else + fail 'It was not possible to safely remove SHUNIT tmp directory.' + fi +} + function test_print_files_authors() { local -r ret=$(print_files_authors "tests/unit/samples/print_file_author_test_dir/code1.c") - multilineAssertEquals "$ret" "$CORRECT_FILE_MSG" + multilineAssertEquals "$CORRECT_FILE_MSG" "$ret" } function test_print_files_authors_from_dir() { local -r ret=$(print_files_authors "tests/unit/samples/print_file_author_test_dir") - multilineAssertEquals "$ret" "$CORRECT_DIR_MSG" + multilineAssertEquals "$CORRECT_DIR_MSG" "$ret" } function test_maintainers_main() { local ret - local -r original_dir="$PWD" ret="$(maintainers_main tests/.tmp)" multilineAssertEquals "$ret" "$CORRECT_TMP_MSG" cd "$FAKE_KERNEL" || { - fail "($LINENO) It was not possible to move to temporary directory" + fail "(${LINENO}) It was not possible to move to temporary directory" return } ret="$(maintainers_main .)" - multilineAssertEquals "$ret" "$CORRECT_TMP_MSG" + multilineAssertEquals "$CORRECT_TMP_MSG" "$ret" ret="$(maintainers_main fs)" - multilineAssertEquals "$ret" "$CORRECT_TMP_FS_MSG" + multilineAssertEquals "$CORRECT_TMP_FS_MSG" "$ret" cd fs || { - fail "($LINENO) It was not possible to move to fs directory" + fail "(${LINENO}) It was not possible to move to fs directory" return } ret="$(maintainers_main ..)" - multilineAssertEquals "$ret" "$CORRECT_TMP_MSG" + multilineAssertEquals "$CORRECT_TMP_MSG" "$ret" ret="$(maintainers_main .)" - multilineAssertEquals "$ret" "$CORRECT_TMP_FS_MSG" - cd "$original_dir" || { - fail "($LINENO) It was not possible to move back from temp directory" + multilineAssertEquals "$CORRECT_TMP_FS_MSG" "$ret" +} + +# This function tests expected behaviors for failure in +# parse_maintainers_options. +function test_parse_maintainers_options() +{ + local return_status + + cd "$FAKE_KERNEL" || { + fail "(${LINENO}) It was not possible to move to temporary directory" return } + + # Invalid option (typo) + unset options_values + declare -gA options_values + parse_maintainers_options --update-pacth # Messy argument + return_status="$?" + assertEquals "(${LINENO})" 22 "$return_status" + + # Invalid option (even with a correct one following it) + unset options_values + declare -gA options_values + parse_maintainers_options --update-patch --invalid-option + return_status="$?" + assertEquals "(${LINENO})" 22 "$return_status" + + # Too many arguments + unset options_values + declare -gA options_values + parse_maintainers_options --update-patch '.' 'some_file' + return_status="$?" + assertEquals "(${LINENO})" 22 "$return_status" +} + +function test_maintainers_main_update_patch_invalid_inputs() +{ + local return_status + local output_from_maintainers_main + + cd "$FAKE_KERNEL" || { + fail "(${LINENO}) It was not possible to move to temporary directory" + return + } + + output_from_maintainers_main="$(maintainers_main --update-patch 'fs/some_file')" + return_status="$?" + assertEquals "(${LINENO})" 'Option --update-patch was passed but given path is not a patch.' "$output_from_maintainers_main" + assertEquals "(${LINENO})" 1 "$return_status" + + output_from_maintainers_main="$(maintainers_main --update-patch 'cover_letter_test.patch')" + return_status="$?" + assertEquals "(${LINENO})" 'Option --update-patch was passed but given path is not a patch.' "$output_from_maintainers_main" + assertEquals "(${LINENO})" 1 "$return_status" +} + +# This function tests cases where the maintainers_main is called outside +# of a kernel tree and the given path is not in a kernel tree either. +function test_maintainers_main_no_kernel() +{ + local return_status + local output_from_maintainers_main + + # Going outside of a kernel tree + cd "$SHUNIT_TMPDIR" || { + fail "(${LINENO}) It was not possible to move to temporary directory" + return + } + mkdir 'not-a-kernel' + + # Testing with default value + output_from_maintainers_main="$(maintainers_main)" + return_status="$?" + assertEquals "(${LINENO})" 'Neither the given path nor the working path is in a kernel tree.' "$output_from_maintainers_main" + assertEquals "(${LINENO})" 1 "$return_status" + + # Testing while giving a path + output_from_maintainers_main="$(maintainers_main ./not-a-kernel)" + return_status="$?" + assertEquals "(${LINENO})" 'Neither the given path nor the working path is in a kernel tree.' "$output_from_maintainers_main" + assertEquals "(${LINENO})" 1 "$return_status" +} + +# This function tests cases where the maintainers_main is called inside +# a kernel tree, but the given path is not a patch and is outside a kernel tree. +function test_maintainers_main_path_out_of_tree() +{ + local return_status + local output_from_maintainers_main + + cd "$FAKE_KERNEL" || { + fail "(${LINENO}) It was not possible to move to temporary directory" + return + } + + output_from_maintainers_main="$(maintainers_main ..)" + return_status="$?" + assertEquals "(${LINENO})" 'The given file is not a patch and is outside a kernel tree.' "$output_from_maintainers_main" + assertEquals "(${LINENO})" 1 "$return_status" +} + +# This function tests cases where the given paths to maintainers_main are invalid +function test_maintainers_main_invalid_paths() +{ + local return_status + local output_from_maintainers_main + + cd "$FAKE_KERNEL" || { + fail "(${LINENO}) It was not possible to move to temporary directory" + return + } + + # Test for files that don't exist in current directory + output_from_maintainers_main="$(maintainers_main 'file-does-not-exist.c')" + return_status="$?" + assertEquals "(${LINENO})" 'Invalid path' "$output_from_maintainers_main" + assertEquals "(${LINENO})" 1 "$return_status" + + # Test for files that don't exist in an other existing directory + output_from_maintainers_main="$(maintainers_main "$SHUNIT_TMPDIR/not-a-file.c")" + return_status="$?" + assertEquals "(${LINENO})" 'Invalid path' "$output_from_maintainers_main" + assertEquals "(${LINENO})" 1 "$return_status" } function test_maintainers_main_patch() { - local original_dir="$PWD" + local return_status + cd "$FAKE_KERNEL" || { - fail "($LINENO) It was not possible to move to temporary directory" + fail "(${LINENO}) It was not possible to move to temporary directory" return } - ret="$(maintainers_main update_patch_test.patch)" - multilineAssertEquals "$ret" "$CORRECT_TMP_MSG" + return_status="$(maintainers_main update_patch_test.patch)" + multilineAssertEquals "$CORRECT_TMP_MSG" "$return_status" # test -u cp -f update_patch_test.patch{,.bak} - ret="$(maintainers_main -u update_patch_test.patch)" - multilineAssertEquals "$ret" "$CORRECT_TMP_PATCH_MSG" + return_status="$(maintainers_main -u update_patch_test.patch)" + multilineAssertEquals "$CORRECT_TMP_PATCH_MSG" "$return_status" assertFileEquals update_patch_test{,_model}.patch cp -f update_patch_test.patch{.bak,} # test --update-patch - ret="$(maintainers_main --update-patch update_patch_test.patch)" - multilineAssertEquals "$ret" "$CORRECT_TMP_PATCH_MSG" + return_status="$(maintainers_main --update-patch update_patch_test.patch)" + multilineAssertEquals "$CORRECT_TMP_PATCH_MSG" "$return_status" assertFileEquals update_patch_test{,_model}.patch # test for already existing maintainers - ret="$(maintainers_main -u update_patch_test.patch)" - multilineAssertEquals "$ret" "$CORRECT_TMP_PATCH_ALREADY_IN_MSG" + return_status="$(maintainers_main -u update_patch_test.patch)" + multilineAssertEquals "$CORRECT_TMP_PATCH_ALREADY_IN_MSG" "$return_status" assertFileEquals update_patch_test{,_model}.patch # test for already existing "To:" field without maintainers - ret="$(maintainers_main -u update_patch_test2.patch)" - multilineAssertEquals "$ret" "$CORRECT_TMP_PATCH2_MSG" + return_status="$(maintainers_main -u update_patch_test2.patch)" + multilineAssertEquals "$CORRECT_TMP_PATCH2_MSG" "$return_status" assertFileEquals update_patch_test{,_model}2.patch - - cd "$original_dir" || { - fail "($LINENO) It was not possible to move back from temp directory" - return - } } invoke_shunit diff --git a/tests/unit/plugins/kernel_install/arch_test.sh b/tests/unit/plugins/kernel_install/arch_test.sh index 8b942c3c5..e9962e4cd 100755 --- a/tests/unit/plugins/kernel_install/arch_test.sh +++ b/tests/unit/plugins/kernel_install/arch_test.sh @@ -128,7 +128,7 @@ function test_generate_arch_temporary_root_file_system_remote_and_not_supported( generate_arch_temporary_root_file_system 'TEST_MODE' "$name" 'remote' 'GRUB' )" - assertEquals "($LINENO)" "$?" 22 + assertEquals "($LINENO)" 22 "$?" } function test_generate_arch_temporary_root_file_system_remote_preferred_root_fs() diff --git a/tests/unit/plugins/kernel_install/utils_test.sh b/tests/unit/plugins/kernel_install/utils_test.sh index 473d9066b..b1df9aa88 100755 --- a/tests/unit/plugins/kernel_install/utils_test.sh +++ b/tests/unit/plugins/kernel_install/utils_test.sh @@ -455,7 +455,7 @@ function test_install_modules() local lib_modules_path_bkp="$LIB_MODULES_PATH" output=$(install_modules "$module_target" 'TEST_MODE') - assert_equals_helper 'We did not find required files' "$LINENO" "$?" 2 + assert_equals_helper 'We did not find required files' "$LINENO" 2 "$?" cd "$test_tmp_file" || { fail "($LINENO) It was not possible to move to temporary directory" @@ -532,10 +532,12 @@ function test_install_kernel_remote() mk_fake_tar_file_to_deploy "$PWD" "$KW_DEPLOY_TMP_FILE" "$name" mkdir -p "${KW_DEPLOY_TMP_FILE}/kw_pkg" touch "${KW_DEPLOY_TMP_FILE}/kw_pkg/kw.pkg.info" - printf 'kernel_name=%s\n' "$name" > "${KW_DEPLOY_TMP_FILE}/kw_pkg/kw.pkg.info" - printf 'kernel_binary_image_file=%s\n' "$kernel_image_name" >> "${KW_DEPLOY_TMP_FILE}/kw_pkg/kw.pkg.info" - printf 'architecture=%s\n' "$architecture" >> "${KW_DEPLOY_TMP_FILE}/kw_pkg/kw.pkg.info" - printf 'previous_kernel_backup=yes\n' >> "${KW_DEPLOY_TMP_FILE}/kw_pkg/kw.pkg.info" + { + printf 'kernel_name=%s\n' "$name" + printf 'kernel_binary_image_file=%s\n' "$kernel_image_name" + printf 'architecture=%s\n' "$architecture" + printf 'previous_kernel_backup=yes\n' + } > "${KW_DEPLOY_TMP_FILE}/kw_pkg/kw.pkg.info" touch "${PWD}/boot/vmlinuz-${name}" output=$(install_kernel 'debian' "$reboot" "$target" 'TEST_MODE') @@ -575,9 +577,11 @@ function test_install_kernel_local() mk_fake_tar_file_to_deploy "$PWD" "$KW_DEPLOY_TMP_FILE" mkdir -p "${KW_DEPLOY_TMP_FILE}/kw_pkg" touch "${KW_DEPLOY_TMP_FILE}/kw_pkg/kw.pkg.info" - printf 'kernel_name=%s\n' "$name" > "${KW_DEPLOY_TMP_FILE}/kw_pkg/kw.pkg.info" - printf 'kernel_binary_image_file=%s\n' "$kernel_image_name" >> "${KW_DEPLOY_TMP_FILE}/kw_pkg/kw.pkg.info" - printf 'architecture=%s\n' "$architecture" >> "${KW_DEPLOY_TMP_FILE}/kw_pkg/kw.pkg.info" + { + printf 'kernel_name=%s\n' "$name" + printf 'kernel_binary_image_file=%s\n' "$kernel_image_name" + printf 'architecture=%s\n' "$architecture" + } > "${KW_DEPLOY_TMP_FILE}/kw_pkg/kw.pkg.info" # Check standard remote kernel installation declare -a cmd_sequence=( @@ -659,19 +663,19 @@ function test_is_filesystem_writable() local expected_cmd output=$(is_filesystem_writable 'ext4' 'TEST_MODE') - assert_equals_helper 'Expected nothing' "$LINENO" "$?" 0 + assert_equals_helper 'Expected nothing' "$LINENO" 0 "$?" output=$(is_filesystem_writable 'xpto-lala' 'TEST_MODE') - assert_equals_helper 'Expected EOPNOTSUPP error' "$LINENO" "$?" 95 + assert_equals_helper 'Expected EOPNOTSUPP error' "$LINENO" 95 "$?" output=$(is_filesystem_writable 'btrfs' 'TEST_MODE') expected_cmd='btrfs property get / ro | grep "ro=false" --silent' - assert_equals_helper 'Expected btrfs property get command' "$LINENO" "$output" "$expected_cmd" + assert_equals_helper 'Expected btrfs property get command' "$LINENO" "$expected_cmd" "$output" AB_ROOTFS_PARTITION="${PWD}/kw" output=$(is_filesystem_writable 'ext4' 'TEST_MODE') expected_cmd="tune2fs -l '$AB_ROOTFS_PARTITION' | grep -q '^Filesystem features: .*read-only.*$'" - assert_equals_helper 'Expected tune2fs command' "$LINENO" "$output" "$expected_cmd" + assert_equals_helper 'Expected tune2fs command' "$LINENO" "$expected_cmd" "$output" } function test_make_root_partition_writable() @@ -686,7 +690,7 @@ function test_make_root_partition_writable() } make_root_partition_writable 'TEST_MODE' )" - assert_equals_helper 'It is writable, do nothing' "$LINENO" "$?" 0 + assert_equals_helper 'It is writable, do nothing' "$LINENO" 0 "$?" # Check ext4 AB_ROOTFS_PARTITION='/xpto/la' diff --git a/tests/unit/plugins/kw_mail/to_cc_cmd_test.sh b/tests/unit/plugins/kw_mail/to_cc_cmd_test.sh index 4e8798a0f..be2978e86 100755 --- a/tests/unit/plugins/kw_mail/to_cc_cmd_test.sh +++ b/tests/unit/plugins/kw_mail/to_cc_cmd_test.sh @@ -35,11 +35,11 @@ function test_to_cc_main() bash "$to_cc_cmd" "$FAKE_CACHE" 'to' '' ret="$?" - assert_equals_helper 'Empty patch path should return an error' "$LINENO" "$ret" 22 + assert_equals_helper 'Empty patch path should return an error' "$LINENO" 22 "$ret" bash "$to_cc_cmd" "$FAKE_CACHE" '' 'to_list' ret="$?" - assert_equals_helper 'Empty to_cc should return an error' "$LINENO" "$ret" 22 + assert_equals_helper 'Empty to_cc should return an error' "$LINENO" 22 "$ret" output="$(bash "$to_cc_cmd" "$FAKE_CACHE" to to_list)" expected="$TO_LIST" diff --git a/tests/unit/pomodoro_test.sh b/tests/unit/pomodoro_test.sh index 00b57e19e..e142287ed 100755 --- a/tests/unit/pomodoro_test.sh +++ b/tests/unit/pomodoro_test.sh @@ -118,63 +118,63 @@ function test_parse_pomodoro() local output parse_pomodoro '-t' '10m' - assert_equals_helper 'Time parser failed (minutes)' "$LINENO" "${options_values['TIMER']}" '10m' + assert_equals_helper 'Time parser failed (minutes)' "$LINENO" '10m' "${options_values['TIMER']}" parse_pomodoro '-t' '333h' - assert_equals_helper 'Time parser failed (hour)' "$LINENO" "${options_values['TIMER']}" '333h' + assert_equals_helper 'Time parser failed (hour)' "$LINENO" '333h' "${options_values['TIMER']}" parse_pomodoro '--set-timer' '234s' - assert_equals_helper 'Time parser failed (sec)' "$LINENO" "${options_values['TIMER']}" '234s' + assert_equals_helper 'Time parser failed (sec)' "$LINENO" '234s' "${options_values['TIMER']}" output=$(parse_pomodoro '--set-timer' '23 s') - assert_equals_helper 'No space' "$LINENO" "$?" '22' + assert_equals_helper 'No space' "$LINENO" 22 "$?" output=$(parse_pomodoro '--set-timer' '234') - assert_equals_helper 'No suffix' "$LINENO" "$?" '22' + assert_equals_helper 'No suffix' "$LINENO" 22 "$?" output=$(parse_pomodoro '--set-timer' 'uum') - assert_equals_helper 'No a number' "$LINENO" "$?" '22' + assert_equals_helper 'No a number' "$LINENO" 22 "$?" parse_pomodoro '--check-timer' - assert_equals_helper 'Show current timebox' "$LINENO" "${options_values['SHOW_TIMER']}" '1' + assert_equals_helper 'Show current timebox' "$LINENO" 1 "${options_values['SHOW_TIMER']}" parse_pomodoro '--tag' 'Something is here' - assert_equals_helper 'Tag requires set timer' "$LINENO" "$?" 22 + assert_equals_helper 'Tag requires set timer' "$LINENO" 22 "$?" parse_pomodoro '--set-timer' '1234s' '--tag' 'Something is here' - assert_equals_helper 'Get tag' "$LINENO" "${options_values['TAG']}" 'Something is here' + assert_equals_helper 'Get tag' "$LINENO" 'Something is here' "${options_values['TAG']}" parse_pomodoro '--set-timer' '1234s' '--tag' ' Extra space ' - assert_equals_helper 'Handle extra space failed' "$LINENO" "${options_values['TAG']}" 'Extra space' + assert_equals_helper 'Handle extra space failed' "$LINENO" 'Extra space' "${options_values['TAG']}" str_sample='com ç -u ^ xpo-la ¬ x--bl' parse_pomodoro '--set-timer' '1234s' '--tag' "$str_sample" - assert_equals_helper 'Handle diverse chars' "$LINENO" "${options_values['TAG']}" "$str_sample" + assert_equals_helper 'Handle diverse chars' "$LINENO" "$str_sample" "${options_values['TAG']}" output=$(parse_pomodoro '--description' 'lala lalala') - assert_equals_helper 'Description requires tag' "$LINENO" "$?" 22 + assert_equals_helper 'Description requires tag' "$LINENO" 22 "$?" output=$(parse_pomodoro '-g' 'Some tag' '--description' 'lala lalala') - assert_equals_helper 'Description requires set timer' "$LINENO" "$?" 22 + assert_equals_helper 'Description requires set timer' "$LINENO" 22 "$?" str_sample='This is just a simple description' parse_pomodoro '--set-timer' '1234s' '--tag' 'Some tag' '-d' "$str_sample" - assert_equals_helper 'Wrong description' "$LINENO" "${options_values['DESCRIPTION']}" "$str_sample" + assert_equals_helper 'Wrong description' "$LINENO" "$str_sample" "${options_values['DESCRIPTION']}" str_sample_spaces=' This is just a simple description ' parse_pomodoro '--set-timer' '1234s' '--tag' 'Some tag' '-d' "$str_sample_spaces" - assert_equals_helper 'Wrong description' "$LINENO" "${options_values['DESCRIPTION']}" "$str_sample" + assert_equals_helper 'Wrong description' "$LINENO" "$str_sample" "${options_values['DESCRIPTION']}" str_sample='Does --comment --lal -u -x xpto-bla and xpto--blablbal' parse_pomodoro '--set-timer' '1234s' '--tag' 'Some tag' '-d' "$str_sample" - assert_equals_helper 'Wrong description' "$LINENO" "${options_values['DESCRIPTION']}" "$str_sample" + assert_equals_helper 'Wrong description' "$LINENO" "$str_sample" "${options_values['DESCRIPTION']}" apostrophe="Let's try something with apostrophe (I'm, you're, we're)" parse_pomodoro '--set-timer' '1234s' '--tag' 'apostrophe' '--description' "$apostrophe" - assert_equals_helper 'Wrong description' "$LINENO" "${options_values['DESCRIPTION']}" "$apostrophe" + assert_equals_helper 'Wrong description' "$LINENO" "$apostrophe" "${options_values['DESCRIPTION']}" parse_pomodoro '--verbose' - assert_equals_helper 'Show a detailed output' "$LINENO" "${options_values['VERBOSE']}" '1' + assert_equals_helper 'Show a detailed output' "$LINENO" 1 "${options_values['VERBOSE']}" } function test_register_data_for_report() @@ -246,7 +246,7 @@ function test_is_tag_already_registered() sqlite3 "${KW_DATA_DIR}/kw.db" -batch "INSERT INTO tag ('name') VALUES ('Tag 0') ;" is_tag_already_registered '' 'Tag 0' - assertEquals "$LINENO: We expect to find Tag 0" "$?" 0 + assertEquals "$LINENO: We expect to find Tag 0" 0 "$?" } function test_get_tag_name() @@ -255,7 +255,7 @@ function test_get_tag_name() local expected get_tag_name '' - assert_equals_helper 'Empty string should be detected' "$LINENO" '22' "$?" + assert_equals_helper 'Empty string should be detected' "$LINENO" 22 "$?" output=$(get_tag_name 'Some tag') expected='Some tag' @@ -274,10 +274,10 @@ function test_get_tag_name() # Try to get an ID out of range get_tag_name 65 - assert_equals_helper 'Out of range' "$LINENO" '22' "$?" + assert_equals_helper 'Out of range' "$LINENO" 22 "$?" get_tag_name -2 - assert_equals_helper 'Out of range' "$LINENO" '22' "$?" + assert_equals_helper 'Out of range' "$LINENO" 22 "$?" } function test_is_valid_argument() diff --git a/tests/unit/report_test.sh b/tests/unit/report_test.sh index 483fc79ce..bd536a365 100755 --- a/tests/unit/report_test.sh +++ b/tests/unit/report_test.sh @@ -65,62 +65,62 @@ function test_parse_report_options() # Default values parse_report_options '--day' expected_result=$(get_today_info '+%Y/%m/%d') - assert_equals_helper 'Get today info' "$LINENO" "${options_values['DAY']}" "$expected_result" + assert_equals_helper 'Get today info' "$LINENO" "$expected_result" "${options_values['DAY']}" parse_report_options '--week' expected_result=$(get_days_of_week) - assert_equals_helper 'Get this week info' "$LINENO" "${options_values['WEEK']}" "$expected_result" + assert_equals_helper 'Get this week info' "$LINENO" "$expected_result" "${options_values['WEEK']}" parse_report_options '--month' expected_result=$(get_today_info '+%Y/%m') - assert_equals_helper 'Get this month info' "$LINENO" "${options_values['MONTH']}" "$expected_result" + assert_equals_helper 'Get this month info' "$LINENO" "$expected_result" "${options_values['MONTH']}" parse_report_options '--year' expected_result=$(get_today_info '+%Y') - assert_equals_helper 'Get this year info' "$LINENO" "${options_values['YEAR']}" "$expected_result" + assert_equals_helper 'Get this year info' "$LINENO" "$expected_result" "${options_values['YEAR']}" parse_report_options '--verbose' - assert_equals_helper 'Show a detailed output' "$LINENO" "${options_values['VERBOSE']}" '1' + assert_equals_helper 'Show a detailed output' "$LINENO" 1 "${options_values['VERBOSE']}" # Values with parameters ## Days ref_date='1999/03/03' parse_report_options "--day=$ref_date" expected_result=$(date_to_format "$ref_date" '+%Y/%m/%d') - assert_equals_helper "$ref_date is a valid date" "$LINENO" "${options_values['DAY']}" "$expected_result" + assert_equals_helper "$ref_date is a valid date" "$LINENO" "$expected_result" "${options_values['DAY']}" ref_date='2022/04/32' output=$(parse_report_options "--day=$ref_date" 2> /dev/null) ret="$?" - assert_equals_helper "$ref_date is an invalid date" "$LINENO" "$ret" 22 + assert_equals_helper "$ref_date is an invalid date" "$LINENO" 22 "$ret" ## Weeks ref_date='1990/04/10' parse_report_options "--week=$ref_date" expected_result=$(get_days_of_week "$ref_date") - assert_equals_helper 'We expected all days of week' "$LINENO" "${options_values['WEEK']}" "$expected_result" + assert_equals_helper 'We expected all days of week' "$LINENO" "$expected_result" "${options_values['WEEK']}" ref_date='2022/04/32' output=$(parse_report_options "--week=$ref_date" 2> /dev/null) ret="$?" - assert_equals_helper "$ref_date is invalid" "$LINENO" "$ret" 22 + assert_equals_helper "$ref_date is invalid" "$LINENO" 22 "$ret" ## Month ref_date='1990/04' parse_report_options "--month=$ref_date" expected_result=$(date_to_format "$ref_date/01" '+%Y/%m') - assert_equals_helper 'We expected 1990/04' "$LINENO" "${options_values['MONTH']}" "$expected_result" + assert_equals_helper 'We expected 1990/04' "$LINENO" "$expected_result" "${options_values['MONTH']}" ref_date='1990/30' output=$(parse_report_options "--month=$ref_date" 2> /dev/null) ret="$?" - assert_equals_helper 'Invalid date' "$LINENO" "$ret" 22 + assert_equals_helper 'Invalid date' "$LINENO" 22 "$ret" # Invalid parameter ref_date='2022/04/12' output=$(parse_report_options "--month=$ref_date --day=$ref_date" 2> /dev/null) ret="$?" - assert_equals_helper 'Invalid date' "$LINENO" "$ret" 22 + assert_equals_helper 'Invalid date' "$LINENO" 22 "$ret" } function test_statistics() @@ -558,7 +558,7 @@ function test_save_data_to() # Try to use an invalid root directory path. output=$(save_data_to '/lala/do/not') ret="$?" - assert_equals_helper 'We expect a root path to be invalid' "$LINENO" "$ret" 1 + assert_equals_helper 'We expect a root path to be invalid' "$LINENO" 1 "$ret" # Try to use an invalid folder path error. output=$(save_data_to '/tmp/folder_not_created/') diff --git a/tests/unit/samples/cover_letter_test.patch b/tests/unit/samples/cover_letter_test.patch new file mode 100644 index 000000000..bc7398ff2 --- /dev/null +++ b/tests/unit/samples/cover_letter_test.patch @@ -0,0 +1,21 @@ +From 2741c6a546f4e0b148f7211def92a8843b9e0972 Mon Sep 17 00:00:00 2001 +From: kw +Date: Fri, 23 May 2023 09:04:44 -0300 +Subject: [PATCH 0/2] Cover letter example + +This is an example of a cover letter for testing purposes. +There are not actual pacthes related to this file and the +following modified files do not exist. + +kw (2): + src: art3435: Add failure paths as corner cases + test: unit: art3435_test: Add failing tests + + src/art3435.c | 34 ++++++++++++++++++ + test/unit/art3435.sh | 86 ++++++++++++++++++++++++++++++++++ + + 2 files changed, 120 insertions(+) + +-- +2.34.1 + diff --git a/tests/unit/samples/kworkflow.config b/tests/unit/samples/kworkflow.config index 980a1fe6e..037f935b2 100644 --- a/tests/unit/samples/kworkflow.config +++ b/tests/unit/samples/kworkflow.config @@ -3,5 +3,7 @@ ssh_ip=127.0.0.1 ssh_port=3333 gui_on=turn on gui_off=turn off +gui_on_after_reboot=turn on after reboot +gui_off_after_reboot=turn off after reboot checkpatch_opts=--no-tree --color=always --strict get_maintainer_opts=--separator , --nokeywords --nogit --nogit-fallback --norolestats diff --git a/tests/unit/samples/print_file_author_test_dir/multiline_authors_test.c b/tests/unit/samples/print_file_author_test_dir/multiline_authors_test.c new file mode 100644 index 000000000..4a62100bb --- /dev/null +++ b/tests/unit/samples/print_file_author_test_dir/multiline_authors_test.c @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * codestyle.c + * + * Copyright (C) 2024 Martha Elsa + * + * This file is used to test the print_files_authors function + * at src/maintainers.sh. + * + */ + +#include + +void ordinary_dummy_function(void) +{ + puts("Just a function..."); +} + +int main(int argc, char *args[]) +{ + int i; + printf("%d\n", argc); + for (i = 0; i < argc; ++i) + printf("%s ", args[i]); + return 0; +} + +MODULE_AUTHOR ( "Martha Elsa " \ + "Verena Bert " ) +ORDINARY_MACRO ( "Does something..." ) +MODULE_AUTHOR ( "Gabi Katinka \n" + "Leonie Hildebert " ) \ No newline at end of file diff --git a/tests/unit/ui/patch_hub/patch_hub_core_test.sh b/tests/unit/ui/patch_hub/patch_hub_core_test.sh index 6146cfb75..9dc31e4c0 100755 --- a/tests/unit/ui/patch_hub/patch_hub_core_test.sh +++ b/tests/unit/ui/patch_hub/patch_hub_core_test.sh @@ -36,7 +36,7 @@ function test_show_dashboard() } show_dashboard - assert_equals_helper 'Expected register screen' "$LINENO" "${screen_sequence['SHOW_SCREEN']}" 'registered_mailing_lists' + assert_equals_helper 'Expected register screen' "$LINENO" 'registered_mailing_lists' "${screen_sequence['SHOW_SCREEN']}" # Mock bookmarked # shellcheck disable=SC2317 @@ -46,7 +46,7 @@ function test_show_dashboard() } show_dashboard - assert_equals_helper 'Expected register screen' "$LINENO" "${screen_sequence['SHOW_SCREEN']}" 'bookmarked_patches' + assert_equals_helper 'Expected register screen' "$LINENO" 'bookmarked_patches' "${screen_sequence['SHOW_SCREEN']}" } function test_list_patches_with_patches() @@ -98,10 +98,10 @@ function test_list_patches_without_patches() target_array_list=() list_patches 'Message test' target_array_list 'show_new_patches_in_the_mailing_list' '' - assert_equals_helper 'Expected screen' "$LINENO" "${screen_sequence['SHOW_SCREEN']}" 'dashboard' + assert_equals_helper 'Expected screen' "$LINENO" 'dashboard' "${screen_sequence['SHOW_SCREEN']}" list_patches 'Message test' target_array_list 'bookmarked_patches' '' - assert_equals_helper 'Expected screen' "$LINENO" "${screen_sequence['SHOW_SCREEN']}" 'dashboard' + assert_equals_helper 'Expected screen' "$LINENO" 'dashboard' "${screen_sequence['SHOW_SCREEN']}" } invoke_shunit diff --git a/tests/unit/utils.sh b/tests/unit/utils.sh index 19bc52671..4697f8845 100755 --- a/tests/unit/utils.sh +++ b/tests/unit/utils.sh @@ -265,6 +265,21 @@ function mk_fake_git() git commit --allow-empty -q -m 'Third commit' } +# Create a new git branch for current local repository and return to master branch afterwards. +# +# @branch_name The name of the new branch to be created. +function mk_git_branch() +{ + local branch_name="$1" + + git checkout --quiet HEAD^ + git checkout --quiet -b "$branch_name" + touch branch_file + git add branch_file + git commit --message "create_branch" --quiet + git checkout --quiet master +} + function mk_fake_kw_folder() { local target_folder="$1"