clang-tidy: readability-else-after-return
[geeqie.git] / scripts / lua-test.sh
1 #!/bin/sh
2 #**********************************************************************
3 # Copyright (C) 2023 - The Geeqie Team
4 #
5 # Author: Colin Clark
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #**********************************************************************
21
22 ## @file
23 ## @brief Run Lua tests
24 ##
25 ## $1 Geeqie executable
26 ##
27 ## Create a basic image and run all lua built-in functions on it.
28 ## The image file and the Lua test file are created within this script.
29
30 if [ -z "$XDG_CONFIG_HOME" ]
31 then
32         config_home="$HOME/.config"
33 else
34         config_home="$XDG_CONFIG_HOME"
35 fi
36
37 lua_test_image=$(mktemp --suffix=".jpeg" "${TMPDIR:-/tmp}/geeqie.XXXXXXXXXX")
38 mkdir -p "$config_home/geeqie/lua/"
39 lua_test_file=$(mktemp --suffix=".lua" --tmpdir="$config_home/geeqie/lua/" lua-test-XXXXXX)
40
41 # Test image generated by:
42 # convert -size 32x32 xc:white empty.jpg
43 # exiftool -ISO=200 --printConv empty.jpg
44 # base64 --wrap=0 empty.jpg > <temp file>
45 lua_test_image_base64="/9j/4AAQSkZJRgABAQAAAQABAAD/4QCkRXhpZgAATU0AKgAAAAgABQEaAAUAAAABAAAASgEbAAUAAAABAAAAUgEoAAMAAAABAAEAAAITAAMAAAABAAEAAIdpAAQAAAABAAAAWgAAAAAAAAABAAAAAQAAAAEAAAABAAWIJwADAAAAAQDIAACQAAAHAAAABDAyMzKRAQAHAAAABAECAwCgAAAHAAAABDAxMDCgAQADAAAAAf//AAAAAAAA/9sAQwADAgICAgIDAgICAwMDAwQGBAQEBAQIBgYFBgkICgoJCAkJCgwPDAoLDgsJCQ0RDQ4PEBAREAoMEhMSEBMPEBAQ/8AACwgAIAAgAQERAP/EABUAAQEAAAAAAAAAAAAAAAAAAAAJ/8QAFBABAAAAAAAAAAAAAAAAAAAAAP/aAAgBAQAAPwCqYAAAAD//2Q=="
46 printf "%s" "$lua_test_image_base64" | base64 --decode > "$lua_test_image"
47
48 lua_test="path = Image:get_path()
49 name = Image:get_name()
50 extension = Image:get_extension()
51 date = math.floor(Image:get_date())
52 size = math.floor(Image:get_size())
53 marks = math.floor(Image:get_marks())
54 exif_structure = Image:get_exif()
55 iso = exif_structure:get_datum(\"Exif.Photo.ISOSpeedRatings\")
56 ret = name..\"\n\"..extension..\"\n\"..path..\"\n\"..date..\"\n\"..size..\"\n\"..marks..\"\n\"..iso
57 return ret
58 "
59 printf  "%s" "$lua_test" > "$lua_test_file"
60
61 xvfb-run --auto-servernum "$1" &
62
63 # Wait for remote to initialize
64 while [ ! -e "$config_home/geeqie/.command" ] ;
65 do
66         sleep 1
67 done
68
69 sleep 2
70
71 base_lua=$(basename "$lua_test_file")
72 result=$(xvfb-run --auto-servernum "$1" --remote --lua:"$lua_test_image","$base_lua")
73 xvfb-run --auto-servernum "$1" --remote --quit
74
75 ## @FIXME Running on GitHub gives additional dbind-WARNINGs. The data required is the last n lines.
76 result_tail=$(printf "%s" "$result" | tail --lines=7)
77
78 expected=$(printf "%s\n.%s\n%s\n%s\n%s\n%s\n%s" "$(basename "$lua_test_image")" jpeg "$lua_test_image" "$(stat -c %Y "$lua_test_image")" "$(stat -c %s "$lua_test_image")" 0 200)
79
80 printf "Result                              Expected\n"
81 printf '%s\n' "$result_tail" "$expected"| pr -2  --omit-header
82
83 rm "$lua_test_image"
84 rm "$lua_test_file"
85
86 if [ "$result_tail" = "$expected" ]
87 then
88         exit 0
89 else
90         exit 1
91 fi
92