1"""
2os module test for the CC3200 based boards
3"""
4
5from machine import SD
6import os
7
8mch = os.uname().machine
9if "LaunchPad" in mch:
10    sd_pins = ("GP16", "GP17", "GP15")
11elif "WiPy" in mch:
12    sd_pins = ("GP10", "GP11", "GP15")
13else:
14    raise Exception("Board not supported!")
15
16sd = SD(pins=sd_pins)
17
18os.mount(sd, "/sd")
19os.mkfs("/sd")
20os.chdir("/flash")
21print(os.listdir())
22
23os.chdir("/sd")
24print(os.listdir())
25
26# create a test directory in flash
27os.mkdir("/flash/test")
28os.chdir("/flash/test")
29print(os.getcwd())
30os.chdir("..")
31print(os.getcwd())
32os.chdir("test")
33print(os.getcwd())
34# create a new file
35f = open("test.txt", "w")
36test_bytes = os.urandom(1024)
37n_w = f.write(test_bytes)
38print(n_w == len(test_bytes))
39f.close()
40f = open("test.txt", "r")
41r = bytes(f.read(), "ascii")
42# check that we can write and read it correctly
43print(r == test_bytes)
44f.close()
45os.rename("test.txt", "newtest.txt")
46print(os.listdir())
47os.rename("/flash/test", "/flash/newtest")
48print(os.listdir("/flash"))
49os.remove("newtest.txt")
50os.chdir("..")
51os.rmdir("newtest")
52
53# create a test directory in the sd card
54os.mkdir("/sd/test")
55os.chdir("/sd/test")
56print(os.getcwd())
57os.chdir("..")
58print(os.getcwd())
59os.chdir("test")
60print(os.getcwd())
61# create a new file
62f = open("test.txt", "w")
63test_bytes = os.urandom(1024)
64n_w = f.write(test_bytes)
65print(n_w == len(test_bytes))
66f.close()
67f = open("test.txt", "r")
68r = bytes(f.read(), "ascii")
69# check that we can write and read it correctly
70print(r == test_bytes)
71f.close()
72
73print("CC3200" in os.uname().machine)
74print("WiPy" == os.uname().sysname)
75
76os.sync()
77os.stat("/flash")
78os.stat("/flash/sys")
79os.stat("/flash/boot.py")
80os.stat("/sd")
81os.stat("/")
82os.chdir("/sd/test")
83os.remove("test.txt")
84os.chdir("/sd")
85os.rmdir("test")
86os.listdir("/sd")
87print(os.listdir("/"))
88os.unmount("/sd")
89print(os.listdir("/"))
90os.mkfs(sd)
91os.mount(sd, "/sd")
92print(os.listdir("/"))
93os.chdir("/flash")
94
95# next ones must raise
96sd.deinit()
97try:
98    os.listdir("/sd")
99except:
100    print("Exception")
101
102# re-initialization must work
103sd.init()
104print(os.listdir("/sd"))
105
106try:
107    os.mount(sd, "/sd")
108except:
109    print("Exception")
110
111try:
112    os.mount(sd, "/sd2")
113except:
114    print("Exception")
115
116os.unmount("/sd")
117try:
118    os.listdir("/sd")
119except:
120    print("Exception")
121
122try:
123    os.unmount("/flash")
124except:
125    print("Exception")
126
127try:
128    os.unmount("/something")
129except:
130    print("Exception")
131
132try:
133    os.unmount("something")
134except:
135    print("Exception")
136
137try:
138    os.mkfs("flash")  # incorrect path format
139except:
140    print("Exception")
141
142try:
143    os.remove("/flash/nofile.txt")
144except:
145    print("Exception")
146
147try:
148    os.rename("/flash/nofile.txt", "/flash/nofile2.txt")
149except:
150    print("Exception")
151
152try:
153    os.chdir("/flash/nodir")
154except:
155    print("Exception")
156
157try:
158    os.listdir("/flash/nodir")
159except:
160    print("Exception")
161
162os.mount(sd, "/sd")
163print(os.listdir("/"))
164os.unmount("/sd")
165