summary refs log blame commit diff stats
path: root/test/tc_ext.py
blob: 495591a18bd4c41a689ba6697b691da17ad40a7c (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15














                                                                       





                                                          

































































                                                                           









                                                               
                                                     
                                               

                                                  
 





                                                                                    





                                                                               

























                                                                                         






                                                                                     
 

                          
# Copyright (C) 2009, 2010  Roman Zimbelmann <romanz@lavabit.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os.path
import sys
rangerpath = os.path.join(os.path.dirname(__file__), '..')
if sys.path[1] != rangerpath:
	sys.path[1:1] = [rangerpath]

import unittest
from collections import deque

from ranger.ext.iter_tools import *

class TestCases(unittest.TestCase):
	def test_flatten(self):
		def f(x):
			return list(flatten(x))

		self.assertEqual(
			[1,2,3,4,5],
			f([1,2,3,4,5]))
		self.assertEqual(
			[1,2,3,4,5],
			f([1,[2,3],4,5]))
		self.assertEqual(
			[1,2,3,4,5],
			f([[1,[2,3]],4,5]))
		self.assertEqual(
			[],
			f([[[[]]]]))
		self.assertEqual(
			['a', 'b', 'fskldfjl'],
			f(['a', ('b', 'fskldfjl')]))
		self.assertEqual(
			['a', 'b', 'fskldfjl'],
			f(['a', deque(['b', 'fskldfjl'])]))
		self.assertEqual(
			set([3.5, 4.3, 5.2, 6.0]),
			set(f([6.0, set((3.5, 4.3)), (5.2, )])))

	def test_unique(self):
		def u(x):
			return list(unique(x))

		self.assertEqual(
			[1,2,3],
			u([1,2,3]))
		self.assertEqual(
			[1,2,3],
			u([1,2,3,2,1]))
		self.assertEqual(
			[1,2,3],
			u([1,2,3,1,2,3,2,2,3,1,2,3,1,2,3,2,3,2,1]))
		self.assertEqual(
			[1,[2,3]],
			u([1,[2,3],1,[2,3],[2,3],1,[2,3],1,[2,3],[2,3],1]))

	def test_unique_keeps_type(self):
		def u(x):
			return unique(x)

		self.assertEqual(
			[1,2,3],
			u([1,2,3,1]))
		self.assertEqual(
			(1,2,3),
			u((1,2,3,1)))
		self.assertEqual(
			set((1,2,3)),
			u(set((1,2,3,1))))
		self.assertEqual(
			deque((1,2,3)),
			u(deque((1,2,3,1))))

	def test_mount_path(self):
		# assuming ismount() is used

		def my_ismount(path):
			depth = path.count('/')
			if path.startswith('/media'):
				return depth == 0 or depth == 2
			return depth <= 1

		from ranger.ext import mount_path
		original_ismount = mount_path.ismount
		mount_path.ismount = my_ismount
		try:
			mp = mount_path.mount_path

			self.assertEqual('/home', mp('/home/hut/porn/bondage'))
			self.assertEqual('/', mp('/'))
			self.assertEqual('/media/sdb1', mp('/media/sdb1/foo/bar'))
			self.assertEqual('/media/sdc2', mp('/media/sdc2/a/b/c/d/e'))
		finally:
			mount_path.ismount = original_ismount

		# TODO: links are not tested but I don't see how its possible
		# without messing around with mounts.
		# self.assertEqual('/media/foo',
		#     mount_path('/media/bar/some_link_to_a_foo_subdirectory'))

	def test_openstruct(self):
		from ranger.ext.openstruct import OpenStruct
		from random import randint, choice
		from string import ascii_letters

		os = OpenStruct(a='a')
		self.assertEqual(os.a, 'a')
		self.assertRaises(AttributeError, getattr, os, 'b')

		dictionary = {'foo': 'bar', 'zoo': 'zar'}
		os = OpenStruct(dictionary)
		self.assertEqual(os.foo, 'bar')
		self.assertEqual(os.zoo, 'zar')
		self.assertRaises(AttributeError, getattr, os, 'sdklfj')

		for i in range(100):
			attr_name = ''.join(choice(ascii_letters) \
				for x in range(randint(3,9)))
			value = randint(100,999)
			if not attr_name in os:
				self.assertRaises(AttributeError, getattr, os, attr_name)
			setattr(os, attr_name, value)
			value2 = randint(100,999)
			setattr(os, attr_name, value2)
			self.assertEqual(value2, getattr(os, attr_name))

	def test_shell_escape(self):
		from ranger.ext.shell_escape import shell_escape, shell_quote
		self.assertEqual(r"'luigi'\''s pizza'", shell_quote("luigi's pizza"))
		self.assertEqual(r"luigi\'s\ pizza", shell_escape("luigi's pizza"))
		self.assertEqual(r"\$lol/foo\\xyz\|\>\<\]\[",
				shell_escape(r"$lol/foo\xyz|><]["))


if __name__ == '__main__':
	unittest.main()
n> Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "RIFLE 1" .TH RIFLE 1 "rifle-1.9.0b6" "01/04/2018" "rifle manual" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" rifle \- ranger's file opener .SH "SYNOPSIS" .IX Header "SYNOPSIS" \&\fBrifle\fR [\fB\-\-help\fR] [\fB\-f\fR \fI\s-1FLAGS\s0\fR] [\fB\-l\fR] [\fB\-p\fR \fI\s-1KEYWORD\s0\fR] [\fB\-w\fR \fI\s-1PROGRAM\s0\fR] [\fB\-c\fR \fI\s-1CONFIG_FILE\s0\fR] \fIfiles\fR .SH "DESCRIPTION" .IX Header "DESCRIPTION" rifle is a powerful file executor that allows for complex file type checking, written to meet the needs of the file manager \fIranger\fR. rifle's strength lies in automatically determining file types, depending on which programs are installed on the system, even without any user interaction. .SH "OPTIONS" .IX Header "OPTIONS" .IP "\fB\-f\fR \fI\s-1FLAGS\s0\fR" 14 .IX Item "-f FLAGS" Specify flags for opening the files. Flags are letters that changes how the program is executed. Any combination of flags will work. Writing uppercase flags will negate the effect of all previously used lowercase flags of the same letter. .Sp Table of all flags: f fork program to background r run program as root, using sudo t run program in a separate terminal, as specified by \f(CW$TERMCMD\fR .IP "\fB\-l\fR" 14 .IX Item "-l" List all possible ways to open the specified files. Each line will contain information in the format of \fIid:label:flags:command\fR. \fIid\fR is the identification number. \fIlabel\fR is an arbitrary string that was specified for this command, \fIflags\fR are the flags that are used by default, and \fIcommand\fR is the command that is going to be executed. .IP "\fB\-p\fR \fI\s-1KEYWORD\s0\fR" 14 .IX Item "-p KEYWORD" Pick a method to open the files. .Sp \&\fI\s-1KEYWORD\s0\fR is either the \s-1ID\s0 number listed by \f(CW\*(C`rifle \-l\*(C'\fR or a string that matches a label in the configuration file. .IP "\fB\-w\fR \fI\s-1PROGRAM\s0\fR" 14 .IX Item "-w PROGRAM" Open the files with the program \fI\s-1PROGRAM\s0\fR .IP "\fB\-c\fR \fI\s-1CONFIG_FILE\s0\fR" 14 .IX Item "-c CONFIG_FILE" Read configuration from \fI\s-1CONFIG_FILE\s0\fR, instead of the default. .IP "\fB\-h\fR, \fB\-\-help\fR" 14 .IX Item "-h, --help" Print a list of options and exit. .SH "FILES" .IX Header "FILES" rifle shares configuration files with ranger, though ranger is not required in order to use rifle. The default configuration file \fIrifle.conf\fR is expected to be at \fI~/.config/ranger/rifle.conf\fR. However, this can be overridden with the \fB\-c\fR option. .PP This file specifies patterns for determining the commands to open files with. The syntax is described in the comments of the default \fIrifle.conf\fR that ships with ranger. To obtain it, you need to run: \f(CW\*(C`ranger \-\-copy\-config=rifle\*(C'\fR .SH "ENVIRONMENT" .IX Header "ENVIRONMENT" .IP "\s-1EDITOR\s0" 8 .IX Item "EDITOR" Determines which editor to use for editing files (in the default \fIrifle.conf\fR). .IP "\s-1PAGER\s0" 8 .IX Item "PAGER" Determines which pager to use for displaying files (in the default \fIrifle.conf\fR). .IP "\s-1TERMCMD\s0" 8 .IX Item "TERMCMD" Determines the terminal emulator command for use with the \fIt\fR flag. It is required that the value is the path to an executable file which accepts the \*(L"\-e \s-1COMMAND\*(R"\s0 argument. .IP "\s-1XDG_CONFIG_HOME\s0" 8 .IX Item "XDG_CONFIG_HOME" Specifies the directory for configuration files. Defaults to \fI\f(CI$HOME\fI/.config\fR. .SH "EXAMPLES" .IX Header "EXAMPLES" List all the different methods: .PP .Vb 4 \& $ rifle \-l helloworld.py \& 0:editor::"$EDITOR" \-\- "$@" \& 1:pager::"$PAGER" \-\- "$@" \& 2:::python \-\- "$1" .Ve .PP Display its content by opening it with \*(L"cat\*(R": .PP .Vb 2 \& $ rifle \-w cat helloworld.py \& print("Hello World!") .Ve .PP Run it by picking the method 2, which calls 'python \*(-- \*(L"$1\*(R"': .PP .Vb 2 \& $ rifle \-p 2 helloworld.py \& Hello World! .Ve .PP Display the file in a pager inside a new terminal, run as root: .PP .Vb 1 \& $ rifle \-p 1 \-f tr helloworld.py .Ve