File size: 3,852 Bytes
94f7dc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import collections
import unittest
from multisource_core import license_header,code_records,evaluation_record,units,doc_records

META={'source_id':'test_source','source_repo':'org/repo','source_revision':'abc',
      'source_path':'model.py','source_url':'https://example.org/model.py','source_group':'example','source_row':-1,'upstream_track':'main'}


class MultiSourceTests(unittest.TestCase):
    def test_unknown_license_is_not_inferred_from_mit_dataset_wrapper(self):
        counters=collections.Counter()
        self.assertFalse(code_records('import torch\nx=torch.tensor([1,2])',META,counters,default_license='MIT',require_inline=True))
        self.assertEqual(counters['missing_original_license_evidence'],1)
    def test_explicit_spdx_is_preserved(self):
        code='# Copyright (c) Example Author\n# SPDX-License-Identifier: Apache-2.0\nimport torch\nx=torch.tensor([1,2])'
        license_id,notice,evidence=license_header(code)
        self.assertEqual(license_id,'Apache-2.0');self.assertIn('Example Author',notice)
        self.assertEqual(evidence,'inline_spdx')
    def test_noncommercial_does_not_become_mit(self):
        code='# Copyright NVIDIA\n# Nvidia Source Code License-NC\nimport torch\nx=torch.tensor([1,2])'
        self.assertEqual(license_header(code)[2],'restricted_or_unsupported_header')
    def test_root_bsd_repository_license_is_used_only_without_conflict(self):
        rows=code_records('import torch\nx=torch.tensor([1,2])',META,collections.Counter(),default_license='BSD-3-Clause')
        self.assertTrue(rows);self.assertEqual(rows[0]['license_id'],'BSD-3-Clause')
    def test_cnn_survives_unsafe_download_entrypoint(self):
        code='''import torch
from torch import nn
from torchvision import datasets
class Net(nn.Module):
 def __init__(self):
  super().__init__()
  self.conv=nn.Conv2d(3,8,3)
 def forward(self,x):
  return self.conv(x)
if __name__=='__main__':
 datasets.CIFAR10('.',download=True)
'''
        rows=code_records(code,META,collections.Counter(),default_license='BSD-3-Clause')
        self.assertTrue(any('cnn_from_scratch_candidate' in r['topics'] for r in rows))
        self.assertTrue(all('download=True' not in r['code'] for r in rows))
    def test_decorator_is_not_lost(self):
        code='import torch\n@torch.no_grad()\ndef predict(x):\n return torch.softmax(x,dim=-1)'
        rows=code_records(code,META,collections.Counter(),default_license='BSD-3-Clause')
        self.assertTrue(rows);self.assertIn('@torch.no_grad()',rows[0]['code'])
    def test_guarded_import_is_never_hoisted(self):
        code='try:\n import optional_module\nexcept ImportError:\n pass\nimport torch\ndef f(x):\n return torch.relu(x)'
        rows=code_records(code,META,collections.Counter(),default_license='BSD-3-Clause')
        self.assertTrue(rows);self.assertNotIn('optional_module',rows[-1]['code'])
    def test_benchmark_has_no_training_split_or_recipe_claim(self):
        r=evaluation_record(META,'case1','An intentionally failing shape problem.','MIT','import torch\nx=torch.tensor([1,2])')
        self.assertTrue(r['benchmark_content']);self.assertEqual(r['split'],'test');self.assertEqual(r['intended_use'],'evaluation')
        self.assertIn('not a working recipe',r['text'])
    def test_secret_and_process_calls_rejected(self):
        rows=code_records("import subprocess\nsubprocess.run(['whoami'])",META,collections.Counter(),default_license='MIT')
        self.assertFalse(rows)
    def test_documentation_does_not_split_python_fences(self):
        text='PyTorch tensor training documentation. '*12+'\n\n```python\nprint(123)\n```\n\nMore PyTorch explanation. '*8
        rows=doc_records(text,META,'BSD-3-Clause',collections.Counter())
        self.assertTrue(rows);self.assertTrue(all('```' not in r['text'] for r in rows))


if __name__=='__main__':unittest.main()