tests/helpers: handle os.listdir() exceptions

os.listdir() can throw exceptions like for eg FileNotFoundError
if the path passed as argument does not exist, so we need to
handle these exceptions otherwise RESULT=unknown is returned.

Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
Signed-off-by: Enric Balletbo i Serra <enric.balletbo@collabora.com>
diff --git a/cros/helpers/sysfs.py b/cros/helpers/sysfs.py
old mode 100644
new mode 100755
index 8e52491..f3abfab
--- a/cros/helpers/sysfs.py
+++ b/cros/helpers/sysfs.py
@@ -18,18 +18,21 @@
         before checking a device path.
     """
     match = 0
-    for devname in os.listdir(path):
-        if check_devtype:
-            fd = open(path + "/" + devname + "/name", "r")
-            devtype = fd.read()
-            fd.close()
-            if not devtype.startswith(name):
-                continue
-        else:
-            if not devname.startswith(name):
-                continue
-        match += 1
-        for filename in files:
-            s.assertEqual(os.path.exists(path + "/" + devname + "/" + filename), 1)
+    try:
+        for devname in os.listdir(path):
+            if check_devtype:
+                fd = open(path + "/" + devname + "/name", "r")
+                devtype = fd.read()
+                fd.close()
+                if not devtype.startswith(name):
+                    continue
+            else:
+                if not devname.startswith(name):
+                    continue
+            match += 1
+            for filename in files:
+                s.assertEqual(os.path.exists(path + "/" + devname + "/" + filename), 1)
+    except IOError as e:
+            self.skipTest("Exception occured: {0}, skipping".format(e.strerror))
     if match == 0:
         s.skipTest("No " + name + " found, skipping")
diff --git a/cros/tests/cros_ec_accel.py b/cros/tests/cros_ec_accel.py
old mode 100644
new mode 100755
index ec063d1..8861eb0
--- a/cros/tests/cros_ec_accel.py
+++ b/cros/tests/cros_ec_accel.py
@@ -54,24 +54,27 @@
         ACCEL_1G_IN_MS2 = 9.8185
         ACCEL_MAG_VALID_OFFSET = 0.25
         match = 0
-        for devname in os.listdir("/sys/bus/iio/devices"):
-            base_path = "/sys/bus/iio/devices/" + devname + "/"
-            fd = open(base_path + "name", "r")
-            devtype = fd.read()
-            if devtype.startswith("cros-ec-accel"):
-                location = read_file(base_path + "location")
-                accel_scale = float(read_file(base_path + "scale"))
-                exp = ACCEL_1G_IN_MS2
-                err = exp * ACCEL_MAG_VALID_OFFSET
-                mag = 0
-                for axis in ["x", "y", "z"]:
-                    axis_path = base_path + "in_accel_" + axis + "_raw"
-                    value = int(read_file(axis_path))
-                    value *= accel_scale
-                    mag += value * value
-                mag = math.sqrt(mag)
-                self.assertTrue(abs(mag - exp) <= err)
-                match += 1
-            fd.close()
+        try:
+            for devname in os.listdir("/sys/bus/iio/devices"):
+                base_path = "/sys/bus/iio/devices/" + devname + "/"
+                fd = open(base_path + "name", "r")
+                devtype = fd.read()
+                if devtype.startswith("cros-ec-accel"):
+                    location = read_file(base_path + "location")
+                    accel_scale = float(read_file(base_path + "scale"))
+                    exp = ACCEL_1G_IN_MS2
+                    err = exp * ACCEL_MAG_VALID_OFFSET
+                    mag = 0
+                    for axis in ["x", "y", "z"]:
+                        axis_path = base_path + "in_accel_" + axis + "_raw"
+                        value = int(read_file(axis_path))
+                        value *= accel_scale
+                        mag += value * value
+                    mag = math.sqrt(mag)
+                    self.assertTrue(abs(mag - exp) <= err)
+                    match += 1
+                fd.close()
+        except IOError as e:
+            self.skipTest("Exception occured: {0}, skipping".format(e.strerror))
         if match == 0:
             self.skipTest("No accelerometer found, skipping")
diff --git a/cros/tests/cros_ec_extcon.py b/cros/tests/cros_ec_extcon.py
old mode 100644
new mode 100755
index ec934b2..b2864b5
--- a/cros/tests/cros_ec_extcon.py
+++ b/cros/tests/cros_ec_extcon.py
@@ -9,26 +9,29 @@
     def test_cros_ec_extcon_usbc_abi(self):
         """ Checks the cros-ec extcon ABI. """
         match = 0
-        for devname in os.listdir("/sys/class/extcon"):
-            devtype = read_file("/sys/class/extcon/" + devname + "/name")
-            if ".spi:ec@0:extcon@" in devtype:
-                self.assertEqual(
-                    os.path.exists("/sys/class/extcon/" + devname + "/state"), 1
-                )
-                for cable in os.listdir("/sys/class/extcon/" + devname):
-                    if cable.startswith("cable"):
-                        self.assertEqual(
-                            os.path.exists(
-                                "/sys/class/extcon/" + devname + "/" + cable + "/name"
-                            ),
-                            1,
-                        )
-                        self.assertEqual(
-                            os.path.exists(
-                                "/sys/class/extcon/" + devname + "/" + cable + "/state"
-                            ),
-                            1,
-                        )
-                        match += 1
+        try:
+            for devname in os.listdir("/sys/class/extcon"):
+                devtype = read_file("/sys/class/extcon/" + devname + "/name")
+                if ".spi:ec@0:extcon@" in devtype:
+                    self.assertEqual(
+                        os.path.exists("/sys/class/extcon/" + devname + "/state"), 1
+                    )
+                    for cable in os.listdir("/sys/class/extcon/" + devname):
+                        if cable.startswith("cable"):
+                            self.assertEqual(
+                                os.path.exists(
+                                    "/sys/class/extcon/" + devname + "/" + cable + "/name"
+                                ),
+                                1,
+                            )
+                            self.assertEqual(
+                                os.path.exists(
+                                    "/sys/class/extcon/" + devname + "/" + cable + "/state"
+                                ),
+                                1,
+                            )
+                            match += 1
+        except IOError as e:
+            self.skipTest("Exception occured: {0}, skipping".format(e.strerror))
         if match == 0:
             self.skipTest("No extcon device found, skipping")
diff --git a/cros/tests/cros_ec_rtc.py b/cros/tests/cros_ec_rtc.py
old mode 100644
new mode 100755
index f8ac511..9f497d2
--- a/cros/tests/cros_ec_rtc.py
+++ b/cros/tests/cros_ec_rtc.py
@@ -12,22 +12,25 @@
         if not is_feature_supported(EC_FEATURE_RTC):
             self.skipTest("EC_FEATURE_RTC not supported, skipping")
         match = 0
-        for devname in os.listdir("/sys/class/rtc"):
-            fd = open("/sys/class/rtc/" + devname + "/name", "r")
-            devtype = fd.read()
-            fd.close()
-            if devtype.startswith("cros-ec-rtc"):
-                files = [
-                    "date",
-                    "hctosys",
-                    "max_user_freq",
-                    "since_epoch",
-                    "time",
-                    "wakealarm",
-                ]
-                match += 1
-                for filename in files:
-                    self.assertEqual(
-                        os.path.exists("/sys/class/rtc/" + devname + "/" + filename), 1
-                    )
+        try:
+            for devname in os.listdir("/sys/class/rtc"):
+                fd = open("/sys/class/rtc/" + devname + "/name", "r")
+                devtype = fd.read()
+                fd.close()
+                if devtype.startswith("cros-ec-rtc"):
+                    files = [
+                        "date",
+                        "hctosys",
+                        "max_user_freq",
+                        "since_epoch",
+                        "time",
+                        "wakealarm",
+                    ]
+                    match += 1
+                    for filename in files:
+                        self.assertEqual(
+                            os.path.exists("/sys/class/rtc/" + devname + "/" + filename), 1
+                        )
+        except IOError as e:
+            self.skipTest("Exception occured: {0}, skipping".format(e.strerror))
         self.assertNotEqual(match, 0)