1# Copyright 2014 Christoph Reiter
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2 of the License, or
6# (at your option) any later version.
7
8import os
9import subprocess
10
11from tests import TestCase, mkstemp
12from quodlibet import util
13
14
15QLDATA_DIR = os.path.join(os.path.dirname(util.get_module_dir()), "data")
16
17
18class _TDesktopFileMixin(object):
19    PATH = None
20
21    def test_filename(self):
22        self.assertTrue(self.PATH.endswith(".desktop.in"))
23
24    def test_validate(self):
25        with open(self.PATH, "rb") as template:
26            desktop_data = template.read()
27
28        # copy to a temp file and strip "_ from translatable entries
29        fd, name = mkstemp(suffix=".desktop")
30        os.close(fd)
31        with open(name, "wb") as temp:
32            new_lines = []
33            for l in desktop_data.splitlines():
34                if l.startswith(b"_"):
35                    l = l[1:]
36                new_lines.append(l)
37            temp.write(b"\n".join(new_lines))
38
39        # pass to desktop-file-validate
40        try:
41            output = subprocess.check_output(
42                ["desktop-file-validate", name], stderr=subprocess.STDOUT)
43        except OSError:
44            # desktop-file-validate not available
45            return
46        except subprocess.CalledProcessError as e:
47            output = e.output
48        finally:
49            os.remove(name)
50
51        if output:
52            raise Exception(output)
53
54
55class TQLDesktopFile(TestCase, _TDesktopFileMixin):
56    PATH = os.path.join(QLDATA_DIR, "io.github.quodlibet.QuodLibet.desktop.in")
57
58
59class TEFDesktopFile(TestCase, _TDesktopFileMixin):
60    PATH = os.path.join(QLDATA_DIR, "io.github.quodlibet.ExFalso.desktop.in")
61