def test_iter_texts(self): extractor = CommandExtractor() for doc in iter_html_docs(TEST_DATA_DIR): nr_txts = 0 for _ in extractor.iter_text_lines(doc): nr_txts += 1 self.assertEqual(NR_TEXTS[doc.url.url], nr_txts)
def test_iter_commands(self): extractor = CommandExtractor() for doc in iter_html_docs(TEST_DATA_DIR): print(doc.url.url) for (line, cmd), correct in zip(extractor.iter_commands(doc), COMMANDS[doc.url.url]): self.assertEqual((line, cmd), correct)
def test_get_command(self): ext = CommandExtractor('find') self.assertEqual(ext.get_command('$ find . -name "*.mp3"'), 'find . -name "*.mp3"') self.assertIsNone(ext.get_command('# ls -hl')) self.assertIsNone(ext.get_command('find a file')) self.assertIsNone(ext.get_command('Find . -name "*.mp3"'))
def test_sudo(self): ext = CommandExtractor('ls') self.assertEqual(ext.get_command('# sudo ls -hl'), 'ls -hl') ext = CommandExtractor('sudo') self.assertEqual( ext.get_command('# sudo -u www vi ~www/htdocs/index.html'), 'sudo -u www vi ~www/htdocs/index.html')
def test_is_command(self): ext = CommandExtractor() self.assertTrue(ext.is_command( u'date -s \u201cYYYY-MM-DD HH:MM:SS\u201d')) self.assertTrue(ext.is_command('uname -a')) self.assertTrue(ext.is_command('git log')) self.assertFalse(ext.is_command('')) self.assertFalse(ext.is_command('when I use:')) self.assertFalse(ext.is_command('is used.')) self.assertFalse(ext.is_command('thank you!!!!')) self.assertFalse(ext.is_command('thanks for sharing')) self.assertFalse(ext.is_command( 'drwxr-xr-x 3 root root 4,0K maj 5 2014 home')) self.assertFalse(ext.is_command( 'ls -al %s' % 'a'*ext.MAX_COMMAND_LENGTH)) self.assertFalse(ext.is_command( ("you can use '-c' & '-w' with wc to obtain number of characters" "and words repectively"))) self.assertFalse(ext.is_command('250 total')) # TODO self.assertTrue(ext.is_command('thanx :)'))
def test_is_command_output(self): ext = CommandExtractor() self.assertTrue(ext.is_command_output( 'drwxr-xr-x 3 root root 4,0K maj 5 2014 home')) self.assertFalse(ext.is_command_output('total 0'))
def test_is_command_name(self): ext = CommandExtractor() self.assertTrue(ext.is_command_name('ls')) self.assertTrue(ext.is_command_name('l')) self.assertTrue(ext.is_command_name('7z')) self.assertTrue(ext.is_command_name('apt-get')) self.assertFalse(ext.is_command_name('')) self.assertFalse(ext.is_command_name('1')) self.assertFalse(ext.is_command_name('22')) self.assertFalse(ext.is_command_name('apt-')) self.assertFalse(ext.is_command_name('-')) self.assertFalse(ext.is_command_name('-ls')) self.assertFalse(ext.is_command_name('apt get')) self.assertFalse(ext.is_command_name('APT-GET'))
def test_has_wanted_command(self): ext = CommandExtractor('xargs') self.assertTrue(ext.has_wanted_command( u'find ./music -name "*.mp3" -print0 | xargs -0 ls')) self.assertFalse(ext.has_wanted_command( u'find ./music -name "*.mp3" -print0 | grep xargs')) ext = CommandExtractor(['du', 'mv']) self.assertTrue(ext.has_wanted_command( u'du -h -s *')) self.assertFalse(ext.has_wanted_command( u'ls -hl')) ext = CommandExtractor(['git commit']) self.assertTrue(ext.has_wanted_command( u'git commit --amend')) self.assertFalse(ext.has_wanted_command( u'git pull origin master'))
def test_iter_commands(self): extractor = CommandExtractor() for doc in iter_html_docs(TEST_DATA_DIR): for (line, cmd), correct in zip(extractor.iter_commands(doc), COMMANDS[doc.url.url]): self.assertEqual((line, cmd), correct)