test-sndfile-metadata-set.py 6.6 KB

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