def METHOD_NAME(self, shell,
                                                has_trailing_newline):
    with tempfile.TemporaryDirectory() as tempdir:
        with mock.patch('os.path.expanduser', return_value=tempdir):
            temp_path = os.path.join(tempdir, *cli.SHELL_FILES[shell])
            os.makedirs(os.path.dirname(temp_path), exist_ok=True)
            existing_file_contents = [
                'something\n',
                'something else' + ('\n' if has_trailing_newline else ''),
            ]
            with open(temp_path, 'w') as f:
                f.writelines(existing_file_contents)
            result = self.invoke(args=['--install-completion', shell])
            expected = cli._create_completion(shell)
            with open(temp_path) as f:
                last_line = f.readlines()[-1]
            self.assertEqual(expected + '\n', last_line)
            self.assertEqual(result.exit_code, 0)