Best Python code snippet using autotest_python
test_VimSwitch.py
Source:test_VimSwitch.py  
...27        self.copyDataToWorkingDir('home/.vim', '.vim')28        exitCode = self.runMain('./vimswitch test/vimrc')29        self.assertEqual(exitCode, 0, stdout.getvalue())30        # Assert default profile is created31        self.assertFileContents('.vimswitch/profiles/default/.vimrc', '" home vimrc data')32        self.assertDirExists('.vimswitch/profiles/default/.vim')33        # Assert home profile is replaced by downloaded profile34        self.assertFileContents('.vimrc', '" test vimrc data')35        self.assertDirExists('.vim')36        # Assert stdout37        self.assertStdout(stdout, """38            Saving profile: default39            Downloading profile from https://github.com/test/vimrc/archive/master.zip40            Switched to profile: test/vimrc41        """)42    @patch('sys.stdout', new_callable=StringIO)43    def test_switchProfile_switchToNonExistantProfile_showsError(self, stdout):44        self.copyDataToWorkingDir('home/.vimrc', '.vimrc')45        self.copyDataToWorkingDir('home/.vim', '.vim')46        exitCode = self.runMain('./vimswitch non_existant_profile')47        self.assertEqual(exitCode, -1, stdout.getvalue())48        # Assert home profile is unchanged49        self.assertFileContents('.vimrc', '" home vimrc data')50        self.assertDirExists('.vim')51        # Assert stdout52        self.assertStdout(stdout, """53            Saving profile: default54            Downloading profile from https://github.com/non_existant_profile/archive/master.zip55            Error: .* 404 File not found56        """)57    @patch('sys.stdout', new_callable=StringIO)58    def test_switchProfile_switchToAnotherProfile(self, stdout):59        # Switch to an initial profile60        self.runMain('./vimswitch test/vimrc')61        self.resetStdout(stdout)62        # Now switch to another profile63        exitCode = self.runMain('./vimswitch test2/vimrc')64        self.assertEqual(exitCode, 0, stdout.getvalue())65        # Assert current profile is now test2/vimrc66        self.assertFileContents('.vimrc', '" test2 vimrc data')67        self.assertDirExists('.vim')68        # Assert stdout69        self.assertStdout(stdout, """70            Saving profile: test/vimrc71            Downloading profile from https://github.com/test2/vimrc/archive/master.zip72            Switched to profile: test2/vimrc73        """)74    @patch('sys.stdout', new_callable=StringIO)75    def test_switchProfile_switchToCachedProfile(self, stdout):76        # Download 2 profiles77        self.runMain('./vimswitch test/vimrc')78        self.runMain('./vimswitch test2/vimrc')79        self.resetStdout(stdout)80        # Switch back to the first profile81        exitCode = self.runMain('./vimswitch test/vimrc')82        self.assertEqual(exitCode, 0, stdout.getvalue())83        # Assert current profile is now test/vimrc84        self.assertFileContents('.vimrc', '" test vimrc data')85        self.assertDirExists('.vim')86        # Assert stdout87        self.assertStdout(stdout, """88            Saving profile: test2/vimrc89            Switched to profile: test/vimrc90        """)91    def test_switchProfile_switchFromEmptyDefaultProfile(self):92        exitCode = self.runMain('./vimswitch test/vimrc')93        self.assertEqual(exitCode, 0)94        # Assert default profile is created and empty95        self.assertDirExists('.vimswitch/profiles/default')96        self.assertDirEmpty('.vimswitch/profiles/default')97        # Assert home profile is replaced by downloaded profile98        self.assertFileContents('.vimrc', '" test vimrc data')99        self.assertDirExists('.vim')100    def test_switchProfile_switchToEmptyDefaultProfile(self):101        # Switch to non-default profile102        self.runMain('./vimswitch test/vimrc')103        # Now switch back to default profile104        exitCode = self.runMain('./vimswitch default')105        self.assertEqual(exitCode, 0)106        # Assert home profile is now empty107        self.assertPathDoesNotExist('.vimrc')108        self.assertPathDoesNotExist('.vim')109    @patch('sys.stdout', new_callable=StringIO)110    def test_switchProfile_switchFromReadOnlyProfileData(self, stdout):111        self.copyDataToWorkingDir('home/.vimrc', '.vimrc')112        self.copyDataToWorkingDir('home/.vim', '.vim')113        self.setReadOnly('.vim/plugin/dummy_plugin.vim', True)114        exitCode = self.runMain('./vimswitch test/vimrc')115        self.assertEqual(exitCode, 0, stdout.getvalue())116        # Assert default profile is created117        self.assertFileContents('.vimswitch/profiles/default/.vimrc', '" home vimrc data')118        self.assertFileContents('.vimswitch/profiles/default/.vim/plugin/dummy_plugin.vim', '" dummy home vim plugin')119        self.assertDirExists('.vimswitch/profiles/default/.vim')120        # Assert home profile is replaced by downloaded profile121        self.assertFileContents('.vimrc', '" test vimrc data')122        self.assertDirExists('.vim')123        # Assert home profile no longer contains read-only file124        self.assertPathDoesNotExist('.vim/plugin/dummy_plugin.vim')125        # Assert stdout126        self.assertStdout(stdout, """127            Saving profile: default128            Downloading profile from https://github.com/test/vimrc/archive/master.zip129            Switched to profile: test/vimrc130        """)131    @patch('sys.stdout', new_callable=StringIO)132    def test_switchProfile_savesChangesToCurrentProfile(self, stdout):133        self.copyDataToWorkingDir('home/.vimrc', '.vimrc')134        self.copyDataToWorkingDir('home/.vim', '.vim')135        self.runMain('./vimswitch test/vimrc')136        self.resetStdout(stdout)137        # Make changes to the test/vimrc profile138        self.createFile('.vimrc', '" updated vimrc data')139        self.deleteDir('.vim')140        exitCode = self.runMain('./vimswitch default')141        self.assertEqual(exitCode, 0, stdout.getvalue())142        # Assert .vimrc changes saved143        self.assertFileContents('.vimswitch/profiles/test.vimrc/.vimrc', '" updated vimrc data')144        # Assert .vim dir deleted145        self.assertPathDoesNotExist('.vimswitch/profiles/test.vimrc/.vim')146        # Assert stdout147        self.assertStdout(stdout, """148            Saving profile: test/vimrc149            Switched to profile: default150        """)151    def test_switchProfile_ignoresNonProfileFiles(self):152        # We will check that the following files and dirs still exist after153        # switching profiles154        filePaths = [155            'test'156            'test.txt'157            '.test'158            '.vimperatorrc'159            '.viminfo'160            '_viminfo'161        ]162        dirPaths = [163            'testDir'164            'testDir.txt'165            '.testDir'166            '.vimperator'167        ]168        for filePath in filePaths:169            self.createFile(filePath, 'test content')170        for dirPath in dirPaths:171            self.createDir(dirPath)172        self.copyDataToWorkingDir('home/.vimrc', '_vimrc')173        self.copyDataToWorkingDir('home/.vim', '_vim')174        exitCode = self.runMain('./vimswitch test/vimrc')175        self.assertEqual(exitCode, 0)176        # Assert all the non-profile files and dirs still exist177        for filePath in filePaths:178            self.assertFileContents(filePath, 'test content')179        for dirPath in dirPaths:180            self.assertDirExists(dirPath)181    def test_switchProfile_switchFromWindowsProfile_movesWindowsProfileDataToCache(self):182        self.copyDataToWorkingDir('home/.vimrc', '_vimrc')183        self.copyDataToWorkingDir('home/.vim', '_vim')184        exitCode = self.runMain('./vimswitch test/vimrc')185        self.assertEqual(exitCode, 0)186        # Assert windows profile files are deleted187        self.assertPathDoesNotExist('_vimrc')188        self.assertPathDoesNotExist('_vim')189        # Assert windows profile moved to cache190        self.assertFileContents('.vimswitch/profiles/default/_vimrc', '" home vimrc data')191        self.assertDirExists('.vimswitch/profiles/default/_vim')192        # Assert home profile is replaced by downloaded profile193        self.assertFileContents('.vimrc', '" test vimrc data')194        self.assertDirExists('.vim')195    # Update profile196    @patch('sys.stdout', new_callable=StringIO)197    def test_updateProfile_redownloadsCachedProfile(self, stdout):198        self.runMain('./vimswitch test/vimrc')199        self.resetStdout(stdout)200        # Update profile on internet201        self.fakeInternetRoot = self.getDataPath('fake_internet2')202        # Now we update test/vimrc203        exitCode = self.runMain('./vimswitch --update test/vimrc')204        self.assertEqual(exitCode, 0, stdout.getvalue())205        self.assertFileContents('.vimrc', '" updated vimrc data')206        self.assertFileContents('.vimswitch/profiles/test.vimrc/.vimrc', '" updated vimrc data')207        self.assertDirExists('.vimswitch/profiles/test.vimrc/.vim')208        self.assertStdout(stdout, """209            Saving profile: test/vimrc210            Downloading profile from https://github.com/test/vimrc/archive/master.zip211            Switched to profile: test/vimrc212        """)213    @patch('sys.stdout', new_callable=StringIO)214    def test_updateProfile_switchesToProfile(self, stdout):215        self.runMain('./vimswitch test/vimrc')216        self.runMain('./vimswitch test2/vimrc')217        self.resetStdout(stdout)218        # Update profile on internet219        self.fakeInternetRoot = self.getDataPath('fake_internet2')220        # Now we update test/vimrc221        exitCode = self.runMain('./vimswitch --update test/vimrc')222        self.assertEqual(exitCode, 0, stdout.getvalue())223        self.assertFileContents('.vimrc', '" updated vimrc data')224        self.assertFileContents('.vimswitch/profiles/test.vimrc/.vimrc', '" updated vimrc data')225        self.assertDirExists('.vimswitch/profiles/test.vimrc/.vim')226        self.assertStdout(stdout, """227            Saving profile: test2/vimrc228            Downloading profile from https://github.com/test/vimrc/archive/master.zip229            Switched to profile: test/vimrc230        """)231    @patch('sys.stdout', new_callable=StringIO)232    def test_updateProfile_noArguments_updatesCurrentProfile(self, stdout):233        self.runMain('./vimswitch test/vimrc')234        self.resetStdout(stdout)235        # Update profile on internet236        self.fakeInternetRoot = self.getDataPath('fake_internet2')237        # Now we update test/vimrc238        exitCode = self.runMain('./vimswitch --update')239        self.assertEqual(exitCode, 0, stdout.getvalue())240        self.assertFileContents('.vimrc', '" updated vimrc data')241        self.assertFileContents('.vimswitch/profiles/test.vimrc/.vimrc', '" updated vimrc data')242        self.assertDirExists('.vimswitch/profiles/test.vimrc/.vim')243        self.assertStdout(stdout, """244            Saving profile: test/vimrc245            Downloading profile from https://github.com/test/vimrc/archive/master.zip246            Switched to profile: test/vimrc247        """)248    @patch('sys.stdout', new_callable=StringIO)249    def test_updateProfile_downloadsUncachedProfile(self, stdout):250        exitCode = self.runMain('./vimswitch --update test/vimrc')251        self.assertEqual(exitCode, 0, stdout.getvalue())252        self.assertFileContents('.vimrc', '" test vimrc data')253        self.assertFileContents('.vimswitch/profiles/test.vimrc/.vimrc', '" test vimrc data')254        self.assertDirExists('.vimswitch/profiles/test.vimrc/.vim')255        self.assertStdout(stdout, """256            Saving profile: default257            Downloading profile from https://github.com/test/vimrc/archive/master.zip258            Switched to profile: test/vimrc259        """)260    @patch('sys.stdout', new_callable=StringIO)261    def test_updateProfile_withDefaultProfile_showsError(self, stdout):262        self.runMain('./vimswitch test/vimrc')263        self.resetStdout(stdout)264        exitCode = self.runMain('./vimswitch --update default')265        self.assertEqual(exitCode, -1)266        self.assertFileContents('.vimrc', '" test vimrc data')267        self.assertFileContents('.vimswitch/profiles/test.vimrc/.vimrc', '" test vimrc data')268        self.assertDirExists('.vimswitch/profiles/test.vimrc/.vim')269        self.assertStdout(stdout, """270            Cannot update default profile271        """)272    @patch('sys.stdout', new_callable=StringIO)273    def test_updateProfile_withDefaultProfileAndNoArguments_showsError(self, stdout):274        exitCode = self.runMain('./vimswitch --update')275        self.assertEqual(exitCode, -1)276        self.assertStdout(stdout, """277            Cannot update default profile278        """)279    # Show current profile280    @patch('sys.stdout', new_callable=StringIO)281    def test_noArguments_showsCurrentProfile(self, stdout):282        self.runMain('./vimswitch test/vimrc')  # Sets current profile to test/vimrc283        self.resetStdout(stdout)284        exitCode = self.runMain('./vimswitch')285        self.assertEqual(exitCode, 0, stdout.getvalue())286        # Assert stdout287        self.assertStdout(stdout, """288            Current profile: test/vimrc289        """)290    @patch('sys.stdout', new_callable=StringIO)291    def test_noArgumentsAndNoCurrentProfile_showsCurrentProfileIsNone(self, stdout):292        exitCode = self.runMain('./vimswitch')293        self.assertEqual(exitCode, 0, stdout.getvalue())294        # Assert stdout295        self.assertStdout(stdout, """296            Current profile: None297        """)298    @patch('sys.stdout', new_callable=StringIO)299    def test_help(self, stdout):300        argsList = [301            './vimswitch -h',302            './vimswitch --help'303        ]304        for args in argsList:305            exitCode = self.runMain(args)306            self.assertEqual(exitCode, -1, stdout.getvalue())307            # Assert stdout308            helpRegex = """309                usage: vimswitch [-h] [-u] [-v] [profile]310                A utility for switching between vim profiles.311                positional arguments:312                  profile313                optional arguments:314                  -h, --help     show this help message and exit315                  -u, --update   download profile again316                  -v, --version  show version317            """318            helpRegex = helpRegex.replace('[', r'\[')319            helpRegex = helpRegex.replace(']', r'\]')320            self.assertStdout(stdout, helpRegex)321            self.resetStdout(stdout)322    @patch('sys.stdout', new_callable=StringIO)323    def test_tooManyArgs_showsErrorMessage(self, stdout):324        exitCode = self.runMain('./vimswitch test/vimrc extra_argument')325        self.assertEqual(exitCode, -1, stdout.getvalue())326        # Assert stdout327        self.assertStdout(stdout, """328            unrecognized arguments: extra_argument329            usage: vimswitch .*330            .*331        """)332    @patch('sys.stdout', new_callable=StringIO)333    def test_version(self, stdout):334        argsList = [335            './vimswitch -v',336            './vimswitch --version'337        ]338        for args in argsList:339            exitCode = self.runMain(args)340            self.assertEqual(exitCode, 0, stdout.getvalue())341            # Assert stdout342            appVersion = vimswitch.version.__version__343            pythonVersion = platform.python_version()344            versionRegex = 'vimswitch %s (python %s)' % (appVersion, pythonVersion)345            versionRegex = versionRegex.replace('(', r'\(')346            versionRegex = versionRegex.replace(')', r'\)')347            self.assertStdout(stdout, versionRegex)348            self.resetStdout(stdout)349            # appVersion must have at least 3 chars350            self.assertTrue(len(appVersion) >= len('0.0'))351    # Helpers352    def runMain(self, args):353        self.resetApplication()354        argv = args.split()355        exitCode = self.vimSwitch.main(argv)356        return exitCode357    def resetApplication(self):358        """359        Resets the state of the application. This needs to be called every time360        before running vimswitch.main()361        """362        self.app = Application()363        self.app.settings = Settings(self.getWorkingDir())364        self.app.fileDownloader = createFakeFileDownloader(self.app, self.fakeInternetRoot)365        self.vimSwitch = VimSwitch(self.app)366        self.vimSwitch.raiseExceptions = True367    def createFile(self, path, contents):368        diskIo = self.app.diskIo369        path = self.getTestPath(path)370        diskIo.createFile(path, contents)371    def createDir(self, path):372        diskIo = self.app.diskIo373        path = self.getTestPath(path)374        diskIo.createDir(path)375    def deleteDir(self, path):376        diskIo = self.app.diskIo377        path = self.getTestPath(path)378        diskIo.deleteDir(path)379    def setReadOnly(self, path, readOnly):380        diskIo = self.app.diskIo381        path = self.getTestPath(path)382        diskIo.setReadOnly(path, readOnly)383    def assertFileContents(self, path, expectedContents):384        diskIo = self.app.diskIo385        path = self.getTestPath(path)386        actualContents = diskIo.getFileContents(path)387        self.assertEqual(actualContents, expectedContents)388    def assertDirExists(self, path):389        diskIo = self.app.diskIo390        path = self.getTestPath(path)391        self.assertTrue(diskIo.dirExists(path))392    def assertDirEmpty(self, path):393        diskIo = self.app.diskIo394        path = self.getTestPath(path)395        self.assertTrue(diskIo.isDirEmpty(path))396    def assertPathDoesNotExist(self, path):397        diskIo = self.app.diskIo...test_fs.py
Source:test_fs.py  
...15            path.parent.mkdir(parents=True, exist_ok=True)16            with path.open('w') as f:17                f.write('\n'.join(contents))18                f.write('\n')19    def assertFileContents(self, path, contents):20        """Assert that the contents of path is contents21        Contents may be specified as a list of strings, one per line, without22        line-breaks.23        """24        if isinstance(contents, (list, tuple)):25            contents = '\n'.join(contents) + '\n'26        with path.open('r') as f:27            self.assertMultiLineEqual(contents, f.read())28class SimpleCombinationTestCase(MergeWheelTestCase):29    files = {30        'a': ('abc', 'def'),31        'b': ('abc', 'ghi'),32    }33    def test_missing_lines(self):34        r = missing_lines(self.a, self.b)35        self.assertEqual(r, ['def\n'])36    def test_merge_record(self):37        merge_RECORD(self.a, self.b)38        self.assertFileContents(self.b, ('abc', 'ghi', 'def'))39class MergeTagsTestCase(MergeWheelTestCase):40    files = {41        'a': ('foo', 'Tag: A'),42        'b': ('foo', 'Tag: B'),43    }44    def test_merge_wheel(self):45        merge_WHEEL(self.a, self.b)46        self.assertFileContents(self.b, ('foo', 'Tag: B', 'Tag: A'))47class UpdateRecordTestCase(MergeWheelTestCase):48    files = {49        'dist-info/RECORD': ('dist-info/FOO,sha256=b5bb9d8014a0f9b1d61e21e796d7'50                             '8dccdf1352f23cd32812f4850b878ae4944c,4',),51        'dist-info/WHEEL': ('foo'),52    }53    def test_fix_merged_record(self):54        fix_merged_RECORD(self.RECORD.parent)55        self.assertFileContents(self.RECORD, (56            'dist-info/FOO,sha256=b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32'57            '812f4850b878ae4944c,4',58            'dist-info/WHEEL,sha256=447fb61fa39a067229e1cce8fc0953bfced53eac85d'59            '1844f5940f51c1fcba725,6',...test_symstore.py
Source:test_symstore.py  
...22        src = path.join(DATA_DIR, histfile_name)23        dest = path.join(self.temp_dir, "history.txt")24        shutil.copyfile(src, dest)25        return dest26    def assertFileContents(self, file_path, content):27        with open(file_path, "r") as f:28            self.assertEqual(f.read(), content)29    def test_new_file(self):30        """31        test adding new trasaction line to an empty (non-existing) file32        """33        store = MockHistStore(path.join(self.temp_dir, "history.txt"))34        history = symstore.History(store)35        history.add("new_line")36        self.assertFileContents(store._history_file,37                                "new_line")38    def test_no_newline(self):39        """40        test adding new trasaction line to a history file41        without newline character at the end42        """43        store = MockHistStore(self._init_hist_file("no_newline.txt"))44        history = symstore.History(store)45        history.add("new_line")46        self.assertFileContents(store._history_file,47                                "original_line\nnew_line")48    def test_trailing_newline(self):49        """50        test adding new trasaction line to a history file51        with trailing newline character52        """53        store = MockHistStore(self._init_hist_file("trailing_newline.txt"))54        history = symstore.History(store)55        history.add("new_line")56        self.assertFileContents(store._history_file,...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
