• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2
3# Copyright (C) 2008-2016 Erik de Castro Lopo <erikd@mega-nerd.com>
4#
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are
9# met:
10#
11#     * Redistributions of source code must retain the above copyright
12#       notice, this list of conditions and the following disclaimer.
13#     * Redistributions in binary form must reproduce the above copyright
14#       notice, this list of conditions and the following disclaimer in
15#       the documentation and/or other materials provided with the
16#       distribution.
17#     * Neither the author nor the names of any contributors may be used
18#       to endorse or promote products derived from this software without
19#       specific prior written permission.
20#
21# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33# Simple test script for the sndfile-metadata-set program.
34
35from __future__ import print_function
36
37try:
38    # py2
39    import commands
40except ImportError:
41    # py3
42    import subprocess as commands
43
44import os, sys
45import time, datetime
46
47class Programs:
48    def __init__ (self, needs_exe):
49        if needs_exe:
50            extension = ".exe"
51        else:
52            extension = ""
53        self.meta_set_prog = "./sndfile-metadata-set" + extension
54        self.meta_get_prog = "./sndfile-metadata-get" + extension
55        self.make_sine_prog = "../examples/make_sine" + extension
56
57    def _run_command (self, should_fail, cmd):
58        status, output = commands.getstatusoutput (cmd)
59        if should_fail and not status:
60            print("\n\nError : command '%s' should have failed." % cmd)
61            print(output)
62            print()
63            sys.exit (1)
64        if not should_fail and status:
65            print("\n\nError : command '%s' should not have failed." % cmd)
66            print(output)
67            print()
68            sys.exit (1)
69        return output
70
71    def meta_set (self, should_fail, args):
72        return self._run_command (should_fail, self.meta_set_prog + " " + args)
73
74    def meta_get (self, should_fail, args):
75        return self._run_command (should_fail, self.meta_get_prog + " " + args)
76
77    def make_sine (self):
78        return os.system (self.make_sine_prog)
79
80    def check_executables (self):
81        for name in [ self.meta_set_prog, self.meta_get_prog, self.make_sine_prog ]:
82            if not (os.path.isfile (name)):
83                print("\n\nError : Can't find executable '%s'. Have you run make?" % name)
84                sys.exit (1)
85
86
87def print_test_name (name):
88    print("    %-30s :" % name, end="")
89
90def assert_info (programs, filename, arg, value):
91    output = programs.meta_get (False, "%s %s" % (arg, filename))
92    if output.find (value) < 0:
93        print("\n\nError : not able to find '%s'." % value)
94        print(output)
95        sys.exit (1)
96    return
97
98
99def test_empty_fail (programs):
100    print_test_name ("Empty fail test")
101    output = programs.meta_set (True, "--bext-description Alpha sine.wav")
102    print("ok")
103
104def test_copy (programs):
105    print_test_name ("Copy test")
106    output = programs.meta_set (False, "--bext-description \"First Try\" sine.wav output.wav")
107    assert_info (programs, "output.wav", "--bext-description", "First Try")
108    print("ok")
109
110def test_update (programs, tests):
111    print_test_name ("Update test")
112    for arg, value in tests:
113        output = programs.meta_set (False, "%s \"%s\" output.wav" % (arg, value))
114        assert_info (programs, "output.wav", arg, value)
115    print("ok")
116
117def test_post_mod (programs, tests):
118    print_test_name ("Post mod test")
119    for arg, value in tests:
120        assert_info (programs, "output.wav", arg, value)
121    print("ok")
122
123def test_auto_date (programs):
124    print_test_name ("Auto date test")
125    output = programs.meta_set (False, "--bext-auto-time-date sine.wav date-time.wav")
126    target = datetime.date.today ().__str__ ()
127    assert_info (programs, "date-time.wav", "--bext-orig-date", target)
128    print("ok")
129
130
131#-------------------------------------------------------------------------------
132
133def test_coding_history (programs):
134    print_test_name ("Coding history test")
135    output = programs.meta_set (False, "--bext-coding-hist \"alpha beta\" output.wav")
136    output = programs.meta_get (False, "--bext-coding-hist output.wav")
137    print("ok")
138
139#-------------------------------------------------------------------------------
140
141def test_rewrite (programs):
142    print_test_name ("Rewrite test")
143    output = programs.meta_set (False, "--bext-originator \"Really, really long string\" output.wav")
144    output = programs.meta_set (False, "--bext-originator \"Short\" output.wav")
145    output = programs.meta_get (False, "--bext-originator output.wav")
146    if output.find ("really long") > 0:
147        print("\n\nError : output '%s' should not contain 'really long'." % output)
148        sys.exit (1)
149    print("ok")
150
151#===============================================================================
152
153test_dir = "programs"
154
155print("\nTesting WAV metadata manipulation:")
156
157if os.path.isdir (test_dir):
158    os.chdir (test_dir)
159
160if len (sys.argv) >= 1 and sys.argv [1].endswith ("mingw32"):
161    needs_exe = True
162else:
163    needs_exe = False
164
165programs = Programs (needs_exe)
166
167programs.check_executables ()
168
169programs.make_sine ()
170if not os.path.isfile ("sine.wav"):
171    print("\n\nError : Can't file file 'sine.wav'.")
172    sys.exit (1)
173
174test_empty_fail (programs)
175test_copy (programs)
176
177tests = [
178    ("--bext-description", "Alpha"), ("--bext-originator", "Beta"), ("--bext-orig-ref", "Charlie"),
179    ("--bext-umid", "Delta"), ("--bext-orig-date", "2001-10-01"),  ("--bext-orig-time", "01:02:03"),
180    ("--str-title", "Echo"), ("--str-artist", "Fox trot")
181    ]
182
183test_auto_date (programs)
184test_update (programs, tests)
185test_post_mod (programs, tests)
186
187test_update (programs, [ ("--str-artist", "Fox") ])
188
189# This never worked.
190# test_coding_history ()
191
192test_rewrite (programs)
193
194
195print()
196
197sys.exit (0)
198
199