<commit_before>#!/usr/bin/env python
# test_psutil.py

import unittest
import os
import sys
import subprocess
import time
from test import test_support

import psutil


PYTHON = os.path.realpath(sys.executable)


class TestCase(unittest.TestCase):
    
    def setUp(self):
        self.proc = None
        
    def tearDown(self):
        if self.proc is not None:
            try:
                psutil.Process(self.proc.pid).kill()
            finally:
                self.proc = None        
    
    def test_get_process_list(self):           
        pids = [x.pid for x in psutil.get_process_list()]
        if hasattr(os, 'getpid'):
            self.assertTrue(os.getpid() in pids)
        
    def test_kill(self):
        devnull = open(os.devnull, 'r+')
        proc = subprocess.Popen(PYTHON, stdout=devnull, stderr=devnull)
        psutil.Process(proc.pid).kill()
        
    def test_pid(self):
        devnull = open(os.devnull, 'r+') 
        self.proc = subprocess.Popen(PYTHON, stdout=devnull, 
                                     stderr=devnull)
        self.assertEqual(psutil.Process(self.proc.pid).pid, self.proc.pid)

    def test_path(self):
        devnull = open(os.devnull, 'r+') 
        self.proc = subprocess.Popen(PYTHON, stdout=devnull, 
                                     stderr=devnull)
        time.sleep(0.1)  # XXX: provisional, fix needed
        self.assertEqual(psutil.Process(self.proc.pid).path, PYTHON)
        
    def test_cmdline(self):
        devnull = open(os.devnull, 'r+')
        self.proc = subprocess.Popen([PYTHON, "-E"],
                                     stdout=devnull, stderr=devnull)
        time.sleep(0.1)  # XXX: provisional, fix needed
        self.assertEqual(psutil.Process(self.proc.pid).cmdline, PYTHON + " -E")

    def test_name(self):
        devnull = open(os.devnull, 'r+') 
        self.proc = subprocess.Popen(PYTHON, stdout=devnull, 
                                     stderr=devnull)
        time.sleep(0.1)  # XXX: provisional, fix needed
        self.assertEqual(psutil.Process(self.proc.pid).name, 
                         os.path.basename(PYTHON))        


def test_main():
    test_suite = unittest.TestSuite()
    test_suite.addTest(unittest.makeSuite(TestCase))
    unittest.TextTestRunner(verbosity=2).run(test_suite)
    try:
        from test.test_support import reap_children
    except ImportError:
        pass
    else:
        reap_children()

if __name__ == '__main__':
    test_main()
    <commit_msg>Defined a new DEVNULL global constant containing the dummy /dev/null file object.<commit_after>#!/usr/bin/env python
# test_psutil.py

import unittest
import os
import sys
import subprocess
import time
from test import test_support

import psutil


PYTHON = os.path.realpath(sys.executable)
DEVNULL = open(os.devnull, 'r+') 


class TestCase(unittest.TestCase):
    
    def setUp(self):
        self.proc = None
        
    def tearDown(self):
        if self.proc is not None:
            try:
                psutil.Process(self.proc.pid).kill()
            finally:
                self.proc = None        
    
    def test_get_process_list(self):           
        pids = [x.pid for x in psutil.get_process_list()]
        if hasattr(os, 'getpid'):
            self.assertTrue(os.getpid() in pids)
        
    def test_kill(self):
        proc = subprocess.Popen(PYTHON, stdout=DEVNULL, stderr=DEVNULL)
        psutil.Process(proc.pid).kill()
        
    def test_pid(self):
        self.proc = subprocess.Popen(PYTHON, stdout=DEVNULL, stderr=DEVNULL)
        self.assertEqual(psutil.Process(self.proc.pid).pid, self.proc.pid)

    def test_path(self):
        self.proc = subprocess.Popen(PYTHON, stdout=DEVNULL, stderr=DEVNULL)
        time.sleep(0.1)  # XXX: provisional, fix needed
        self.assertEqual(psutil.Process(self.proc.pid).path, PYTHON)
        
    def test_cmdline(self):
        self.proc = subprocess.Popen([PYTHON, "-E"], stdout=DEVNULL, stderr=DEVNULL)
        time.sleep(0.1)  # XXX: provisional, fix needed
        self.assertEqual(psutil.Process(self.proc.pid).cmdline, PYTHON + " -E")

    def test_name(self):
        self.proc = subprocess.Popen(PYTHON, stdout=DEVNULL,  stderr=DEVNULL)
        time.sleep(0.1)  # XXX: provisional, fix needed
        self.assertEqual(psutil.Process(self.proc.pid).name, 
                         os.path.basename(PYTHON))        


def test_main():
    test_suite = unittest.TestSuite()
    test_suite.addTest(unittest.makeSuite(TestCase))
    unittest.TextTestRunner(verbosity=2).run(test_suite)
    test_support.reap_children()
    DEVNULL.close()

if __name__ == '__main__':
    test_main()
    
{'fraction_non_alphanumeric': 0.11348578756144476, 'fraction_numerical': 0.002992092327420389, 'mean_word_length': 2.236514522821577, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6772785', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1504, 'n_words': 279}