#!/usr/bin/env python

import sys

import six

try:
    from mock import patch
except ImportError:
    from unittest.mock import patch

from csvkit.utilities.csvstat import CSVStat, launch_new_instance
from tests.utils import CSVKitTestCase, ColumnsTests, NamesTests


class TestCSVStat(CSVKitTestCase, ColumnsTests, NamesTests):
    Utility = CSVStat

    def test_launch_new_instance(self):
        with patch.object(sys, 'argv', ['csvstack', 'examples/dummy.csv']):
            launch_new_instance()

    def test_runs(self):
        args = ['examples/test_utf8.csv']
        output_file = six.StringIO()

        utility = CSVStat(args, output_file)
        utility.main()

    def test_encoding(self):
        args = ['-e', 'latin1', 'examples/test_latin1.csv']
        output_file = six.StringIO()

        utility = CSVStat(args, output_file)
        utility.main()

    def test_no_header_row(self):
        args = ['-H', '-c', '2', 'examples/no_header_row.csv']
        output_file = six.StringIO()

        utility = CSVStat(args, output_file)
        utility.main()

        stats = output_file.getvalue()

        self.assertFalse('A' in stats)
        self.assertTrue('B' in stats)

    def test_count_only(self):
        args = ['--count', 'examples/dummy.csv']
        output_file = six.StringIO()

        utility = CSVStat(args, output_file)
        utility.main()

        stats = output_file.getvalue()

        self.assertEqual(stats, 'Row count: 1\n')

@@ -433,18 +433,17 @@
 %5B'csvsta
-ck
+t
 ', 'exam
@@ -529,23 +529,32 @@
         
-args = 
+self.get_output(
 %5B'exampl
@@ -575,113 +575,8 @@
 sv'%5D
-%0A        output_file = six.StringIO()%0A%0A        utility = CSVStat(args, output_file)%0A        utility.main(
 )%0A%0A 
@@ -611,23 +611,32 @@
         
-args = 
+self.get_output(
 %5B'-e', '
@@ -675,113 +675,8 @@
 sv'%5D
-%0A        output_file = six.StringIO()%0A%0A        utility = CSVStat(args, output_file)%0A        utility.main(
 )%0A%0A 
@@ -716,23 +716,41 @@
         
-args = 
+output = self.get_output(
 %5B'-H', '
@@ -792,155 +792,9 @@
 sv'%5D
-%0A        output_file = six.StringIO()%0A%0A        utility = CSVStat(args, output_file)%0A        utility.main()%0A%0A        stats = output_file.getvalue()%0A
+)
 %0A   
@@ -822,21 +822,22 @@
 ('A' in 
-stats
+output
 )%0A      
@@ -861,21 +861,22 @@
 ('B' in 
-stats
+output
 )%0A%0A    d
@@ -913,15 +913,33 @@
     
-args = 
+output = self.get_output(
 %5B'--
@@ -971,155 +971,9 @@
 sv'%5D
-%0A        output_file = six.StringIO()%0A%0A        utility = CSVStat(args, output_file)%0A        utility.main()%0A%0A        stats = output_file.getvalue()%0A
+)
 %0A   
@@ -998,13 +998,14 @@
 ual(
-stats
+output
 , 'R

Reduce duplication in test_csvstat.py