test_metadata_license.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Checks related to the patch's LIC_FILES_CHKSUM metadata variable
  2. #
  3. # Copyright (C) 2016 Intel Corporation
  4. #
  5. # SPDX-License-Identifier: GPL-2.0-only
  6. import base
  7. import os
  8. from data import PatchTestInput
  9. class License(base.Metadata):
  10. metadata = 'LICENSE'
  11. invalid_license = 'PATCHTESTINVALID'
  12. def setUp(self):
  13. # these tests just make sense on patches that can be merged
  14. if not PatchTestInput.repo.canbemerged:
  15. self.skip('Patch cannot be merged')
  16. def test_license_presence(self):
  17. if not self.added:
  18. self.skip('No added recipes, skipping test')
  19. # TODO: this is a workaround so we can parse the recipe not
  20. # containing the LICENSE var: add some default license instead
  21. # of INVALID into auto.conf, then remove this line at the end
  22. auto_conf = os.path.join(os.environ.get('BUILDDIR'), 'conf', 'auto.conf')
  23. open_flag = 'w'
  24. if os.path.exists(auto_conf):
  25. open_flag = 'a'
  26. with open(auto_conf, open_flag) as fd:
  27. for pn in self.added:
  28. fd.write('LICENSE ??= "%s"\n' % self.invalid_license)
  29. no_license = False
  30. for pn in self.added:
  31. rd = self.tinfoil.parse_recipe(pn)
  32. license = rd.getVar(self.metadata)
  33. if license == self.invalid_license:
  34. no_license = True
  35. break
  36. # remove auto.conf line or the file itself
  37. if open_flag == 'w':
  38. os.remove(auto_conf)
  39. else:
  40. fd = open(auto_conf, 'r')
  41. lines = fd.readlines()
  42. fd.close()
  43. with open(auto_conf, 'w') as fd:
  44. fd.write(''.join(lines[:-1]))
  45. if no_license:
  46. self.fail('Recipe does not have the LICENSE field set.')