2
2
3
3
import io
4
4
import os
5
+ import stat
5
6
from pathlib import Path
6
- from tempfile import NamedTemporaryFile as NamedTemp
7
+ from tempfile import NamedTemporaryFile
7
8
from typing import Any
8
9
from typing import List
9
10
10
11
import pytest
11
- from pytest import raises
12
12
13
13
import fgpyo .io as fio
14
14
from fgpyo .io import assert_path_is_writable
18
18
def test_assert_path_is_readable_missing_file_error () -> None :
19
19
"""Error when file does not exist"""
20
20
path = Path ("error.txt" )
21
- with raises (AssertionError ):
21
+
22
+ with pytest .raises (AssertionError ):
22
23
fio .assert_path_is_readable (path = path )
23
24
24
25
25
26
def test_assert_path_is_readable_mode_error () -> None :
26
27
"""Error when permissions are write only by owner"""
27
- with NamedTemp (suffix = ".txt" , mode = "r" , delete = True ) as write_file :
28
+ with NamedTemporaryFile (suffix = ".txt" , mode = "r" , delete = True ) as write_file :
28
29
path = Path (write_file .name )
29
- os .chmod (path , 0o00200 ) # Write only permissions
30
- with raises (AssertionError ):
30
+ os .chmod (path , stat .S_IWUSR ) # Write only permissions
31
+
32
+ with pytest .raises (AssertionError ):
31
33
fio .assert_path_is_readable (path = path )
32
34
33
35
34
36
def test_assert_path_is_readable_pass () -> None :
35
37
"""Returns none when no assertions are violated"""
36
- with NamedTemp (suffix = ".txt" , mode = "w" , delete = True ) as write_file :
38
+ with NamedTemporaryFile (suffix = ".txt" , mode = "w" , delete = True ) as write_file :
37
39
path = Path (write_file .name )
38
40
fio .assert_path_is_readable (path = path )
39
41
40
42
41
43
def test_assert_directory_exists_error () -> None :
42
44
"""Ensure OSError when directory does not exist"""
43
45
path = Path ("/non/existent/dir/" )
44
- with raises (AssertionError ):
46
+ with pytest . raises (AssertionError ):
45
47
fio .assert_directory_exists (path )
46
48
47
49
48
50
def test_assert_directory_exists_pass () -> None :
49
51
"""Asserts fio._assert_directory_exists() returns True when directory exists"""
50
- with NamedTemp (suffix = ".txt" , mode = "w" , delete = True ) as write_file :
52
+ with NamedTemporaryFile (suffix = ".txt" , mode = "w" , delete = True ) as write_file :
51
53
path = Path (write_file .name )
52
54
fio .assert_directory_exists (path = path .parent .absolute ())
53
55
54
56
55
57
def test_assert_path_is_writable_mode_error () -> None :
56
58
"""Error when permissions are read only by owner"""
57
- with NamedTemp (suffix = ".txt" , mode = "w" , delete = True ) as read_file :
59
+ with NamedTemporaryFile (suffix = ".txt" , mode = "w" , delete = True ) as read_file :
58
60
path = Path (read_file .name )
59
- os .chmod (path , 0o00400 ) # Read only permissions
60
- with raises (AssertionError , match = f"File exists but is not writable: { path } " ):
61
+ os .chmod (path , stat . S_IRUSR ) # Read only permissions
62
+ with pytest . raises (AssertionError , match = f"File exists but is not writable: { path } " ):
61
63
assert_path_is_writable (path = path )
62
64
63
65
64
66
def test_assert_path_is_writable_parent_not_writable () -> None :
65
67
"""Error when parent_must_exist is false and no writable parent directory exists"""
66
68
path = Path ("/no/parent/exists/" )
67
- with raises (AssertionError , match = f"File does not have a writable parent directory: { path } " ):
69
+ with pytest . raises (AssertionError , match = "Parent directory is not writable: / " ):
68
70
assert_path_is_writable (path = path , parent_must_exist = False )
69
71
70
72
71
73
def test_assert_path_is_writable_file_does_not_exist () -> None :
72
74
"""Error when file does not exist"""
73
75
path = Path ("example/non_existent_file.txt" )
74
- with raises (
75
- AssertionError ,
76
- match = "Path does not exist and parent isn't extant/writable: example/non_existent_file.txt" ,
77
- ):
76
+ with pytest .raises (AssertionError , match = "Parent directory does not exist:" ):
78
77
assert_path_is_writable (path = path )
79
78
80
79
81
80
def test_assert_path_is_writable_pass () -> None :
82
81
"""Should return the correct writable path"""
83
- with NamedTemp (suffix = ".txt" , mode = "w" , delete = True ) as read_file :
82
+ with NamedTemporaryFile (suffix = ".txt" , mode = "w" , delete = True ) as read_file :
84
83
path = Path (read_file .name )
85
84
assert_path_is_writable (path = path )
86
85
@@ -106,7 +105,7 @@ def test_reader(
106
105
expected : Any ,
107
106
) -> None :
108
107
"""Tests fgpyo.io.to_reader"""
109
- with NamedTemp (suffix = suffix , mode = "r" , delete = True ) as read_file :
108
+ with NamedTemporaryFile (suffix = suffix , mode = "r" , delete = True ) as read_file :
110
109
with fio .to_reader (path = Path (read_file .name )) as reader :
111
110
assert isinstance (reader , expected )
112
111
@@ -123,7 +122,7 @@ def test_writer(
123
122
expected : Any ,
124
123
) -> None :
125
124
"""Tests fgpyo.io.to_writer()"""
126
- with NamedTemp (suffix = suffix , mode = "w" , delete = True ) as write_file :
125
+ with NamedTemporaryFile (suffix = suffix , mode = "w" , delete = True ) as write_file :
127
126
with fio .to_writer (path = Path (write_file .name )) as writer :
128
127
assert isinstance (writer , expected )
129
128
@@ -137,7 +136,7 @@ def test_read_and_write_lines(
137
136
list_to_write : List [Any ],
138
137
) -> None :
139
138
"""Test fgpyo.fio.read_lines and write_lines"""
140
- with NamedTemp (suffix = suffix , mode = "w" , delete = True ) as read_file :
139
+ with NamedTemporaryFile (suffix = suffix , mode = "w" , delete = True ) as read_file :
141
140
fio .write_lines (path = Path (read_file .name ), lines_to_write = list_to_write )
142
141
read_back = fio .read_lines (path = Path (read_file .name ))
143
142
assert next (read_back ) == list_to_write [0 ]
0 commit comments