| 1 |
#!/usr/bin/env python3 |
| 2 |
# Copyright: 2026, Maximiliano Curia <[email protected]> |
| 3 |
# |
| 4 |
# License: ISC |
| 5 |
# Permission to use, copy, modify, and/or distribute this software for any |
| 6 |
# purpose with or without fee is hereby granted, provided that the above |
| 7 |
# copyright notice and this permission notice appear in all copies. |
| 8 |
# . |
| 9 |
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH |
| 10 |
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 11 |
# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 12 |
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 13 |
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE |
| 14 |
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 15 |
# PERFORMANCE OF THIS SOFTWARE. |
| 16 |
|
| 17 |
"""End-to-end: a compound SPDX-License-Identifier tag yields per-atom stanzas. |
| 18 |
|
| 19 |
A source file carrying `SPDX-License-Identifier: MIT or Apache-2.0` must produce |
| 20 |
a Files paragraph whose License field is the canonicalised disjunction |
| 21 |
(`Apache-2 or Expat`) and one standalone License stanza per atom, not a single |
| 22 |
paragraph keyed by the whole expression string. |
| 23 |
""" |
| 24 |
|
| 25 |
import contextlib |
| 26 |
import io |
| 27 |
import unittest |
| 28 |
|
| 29 |
from decopy import cmdoptions |
| 30 |
from decopy.decopy import prepare_output_groups |
| 31 |
from decopy.dep5doc import dep5, output |
| 32 |
from decopy.dep5doc.renderer import renderer_for |
| 33 |
from decopy.fstree import tree |
| 34 |
|
| 35 |
|
| 36 |
def _generate(path: str) -> str: |
| 37 |
options = cmdoptions.process_options(["--no-progress", "--root", path]) |
| 38 |
filetree = tree.RootInfo.build(options) |
| 39 |
copyright_ = dep5.read_copyright(options) |
| 40 |
copyright_.process(filetree) |
| 41 |
filetree.process(options) |
| 42 |
copyright_.remove_misplaced_files(options) |
| 43 |
groups = prepare_output_groups(filetree, copyright_, options) |
| 44 |
buf = io.StringIO() |
| 45 |
with contextlib.redirect_stdout(buf): |
| 46 |
output.generate_output( |
| 47 |
groups, filetree, copyright_, options, renderer_for(options) |
| 48 |
) |
| 49 |
return buf.getvalue() |
| 50 |
|
| 51 |
|
| 52 |
class TestSpdxCompoundEndToEnd(unittest.TestCase): |
| 53 |
def test_compound_field_and_per_atom_stanzas(self): |
| 54 |
out = _generate("tests/testdata/spdxcompound") |
| 55 |
|
| 56 |
# Files paragraph: canonicalised disjunction, not the raw SPDX ids. |
| 57 |
self.assertIn("License: Apache-2 or Expat", out) |
| 58 |
self.assertNotIn("MIT or Apache-2.0", out) |
| 59 |
|
| 60 |
# The compound appears only once -- as the Files field, never as its own |
| 61 |
# standalone stanza. |
| 62 |
self.assertEqual(out.count("License: Apache-2 or Expat"), 1) |
| 63 |
|
| 64 |
# One standalone License stanza per atom (each on its own header line). |
| 65 |
self.assertIn("\nLicense: Apache-2\n", out) |
| 66 |
self.assertIn("\nLicense: Expat\n", out) |
| 67 |
|
| 68 |
|
| 69 |
if __name__ == "__main__": |
| 70 |
unittest.main() |