From dbac6af0e1ee21830d03ca28783bc03dad12b8ba Mon Sep 17 00:00:00 2001
From: Amandine PERRIN <amandine.perrin@pasteur.fr>
Date: Mon, 2 Jan 2023 19:08:44 +0100
Subject: [PATCH] Use number of available cpu instead of total number of cpus
 in tests

---
 test/test_functional/test_align-parser.py     | 20 +++++++++-----
 test/test_functional/test_all-parser.py       |  8 ++++--
 test/test_functional/test_pangenome-parser.py | 21 ++++++++++-----
 test/test_functional/test_prepare-parser.py   | 12 +++++++--
 test/test_functional/test_tree-parser.py      | 26 +++++++++++++------
 test/test_unit/test_utils-argparse.py         |  5 +++-
 6 files changed, 67 insertions(+), 25 deletions(-)

diff --git a/test/test_functional/test_align-parser.py b/test/test_functional/test_align-parser.py
index d723daca..324faf9f 100755
--- a/test/test_functional/test_align-parser.py
+++ b/test/test_functional/test_align-parser.py
@@ -6,6 +6,7 @@ Functional tests for the parser of align subcommand
 """
 import argparse
 import pytest
+import os
 
 from PanACoTA.subcommands import align
 
@@ -23,7 +24,8 @@ def test_parser_noarg(capsys):
     assert "usage: " in err
     assert "-c COREPERS -l LIST_GENOMES -n DATASET_NAME -d DBPATH" in err
     assert "-o OUTDIR" in err
-    assert "[--threads THREADS] [-F] [-P] [-v] [-q] [-h]" in err
+    assert "[--threads THREADS]" in err
+    assert " [-F]" in err #" [-P] [-v] [-q]" in err
     assert "[-h]" in err
     assert "the following arguments are required: -c, -l, -n, -d, -o" in err
 
@@ -46,16 +48,19 @@ def test_parser_thread_toomany(capsys):
     it returns the expected error message
     """
     import multiprocessing
-    nb = multiprocessing.cpu_count()
+    try:
+        nb_cpu = len(os.sched_getaffinity(0))
+    except AttributeError:
+        nb_cpu = multiprocessing.cpu_count()
     parser = argparse.ArgumentParser(description="Align families", add_help=False)
     align.build_parser(parser)
     with pytest.raises(SystemExit):
         align.parse(parser,
                     "-l listgenome -n dname -d dbpath -o outdir "
-                    "--threads {}".format(nb + 3).split())
+                    "--threads {}".format(nb_cpu + 3).split())
     _, err = capsys.readouterr()
     assert ("You have {} threads on your computer, you cannot ask for more: "
-            "invalid value: {}".format(nb, nb+3)) in err
+            "invalid value: {}".format(nb_cpu, nb_cpu+3)) in err
 
 
 def test_parser_thread_neg(capsys):
@@ -98,7 +103,10 @@ def test_parser_allthreads():
     computer
     """
     import multiprocessing
-    nb = multiprocessing.cpu_count()
+    try:
+        nb_cpu = len(os.sched_getaffinity(0))
+    except AttributeError:
+        nb_cpu = multiprocessing.cpu_count()
     parser = argparse.ArgumentParser(description="Align families", add_help=False)
     align.build_parser(parser)
     options = align.parse(parser, "-c cp -l listgenome -n dname -d dbpath -o outdir "
@@ -108,7 +116,7 @@ def test_parser_allthreads():
     assert options.dataset_name == "dname"
     assert options.dbpath == "dbpath"
     assert options.outdir == "outdir"
-    assert options.threads == nb
+    assert options.threads == nb_cpu
     assert options.force is False
     assert options.verbose == 0
     assert options.quiet is False
diff --git a/test/test_functional/test_all-parser.py b/test/test_functional/test_all-parser.py
index d9c4f53a..59b553a2 100644
--- a/test/test_functional/test_all-parser.py
+++ b/test/test_functional/test_all-parser.py
@@ -6,6 +6,7 @@ Functional tests for the parser of 'all' subcommand
 """
 import argparse
 import pytest
+import os
 
 from PanACoTA.subcommands import all_modules as allm
 
@@ -108,14 +109,17 @@ def test_parser_conffile_and_cmd():
     the value kept is the one in cmd.
     """
     import multiprocessing
-    nb = multiprocessing.cpu_count()
+    try:
+        nb_cpu = len(os.sched_getaffinity(0))
+    except AttributeError:
+        nb_cpu = multiprocessing.cpu_count()
     parser = argparse.ArgumentParser(description="Run all modules", add_help=False)
     allm.build_parser(parser)
     options = allm.parse(parser,
                          "-c test/data/all/init_files/default-conffigfile.ini -o out-all "
                          "-n TEST --threads 0 -i 0.99 -Mu".split())
     assert options.outdir == "out-all"
-    assert options.threads == nb
+    assert options.threads == nb_cpu
     assert not options.ncbi_species_taxid
     assert options.prodigal_only == False
     assert options.norefseq
diff --git a/test/test_functional/test_pangenome-parser.py b/test/test_functional/test_pangenome-parser.py
index 0d0d9b5b..a14b349a 100755
--- a/test/test_functional/test_pangenome-parser.py
+++ b/test/test_functional/test_pangenome-parser.py
@@ -7,6 +7,8 @@ Functional tests for the parser of 'pangenome' subcommand
 
 import pytest
 import argparse
+import os
+
 
 from PanACoTA.subcommands import pangenome
 
@@ -25,7 +27,8 @@ def test_parser_noarg(capsys):
     assert "-l LSTINFO_FILE -n DATASET_NAME -d DBPATH" in err
     assert "[-i MIN_ID]" in err
     assert " -o OUTDIR" in err
-    assert "[-f OUTFILE] [-c {0,1,2}]" in err
+    assert "[-f OUTFILE]" in err
+    assert " [-c {0,1,2}]" in err
     assert "[-s SPEDIR] [--threads THREADS] [-v]" in err
     assert "[-q] [-h]" in err
     assert "the following arguments are required: -l, -n, -d, -o" in err
@@ -101,14 +104,17 @@ def test_thread_too_many(capsys):
     it returns the expected error message.
     """
     import multiprocessing
-    nb = multiprocessing.cpu_count()
+    try:
+        nb_cpu = len(os.sched_getaffinity(0))
+    except AttributeError:
+        nb_cpu = multiprocessing.cpu_count()
     parser = argparse.ArgumentParser(description="Do pangenome", add_help=False)
     pangenome.build_parser(parser)
     with pytest.raises(SystemExit):
-        pangenome.parse(parser, f"-l lstinfo -n TEST4 -d dbpath -o od --threads {nb*10}".split())
+        pangenome.parse(parser, f"-l lstinfo -n TEST4 -d dbpath -o od --threads {nb_cpu*10}".split())
     _, err = capsys.readouterr()
     assert ("You have {} threads on your computer, you cannot ask for more: "
-            "invalid value: {}").format(nb, nb*10) in err
+            "invalid value: {}").format(nb_cpu, nb_cpu*10) in err
 
 
 def test_thread_neg(capsys):
@@ -152,7 +158,10 @@ def test_parser_all_threads():
     number of threads.
     """
     import multiprocessing
-    nb = multiprocessing.cpu_count()
+    try:
+        nb_cpu = len(os.sched_getaffinity(0))
+    except AttributeError:
+        nb_cpu = multiprocessing.cpu_count()
     parser = argparse.ArgumentParser(description="Do pangenome", add_help=False)
     pangenome.build_parser(parser)
     options = pangenome.parse(parser, "-l lstinfo -n TEST4 -d dbpath -o od --threads 0".split())
@@ -163,7 +172,7 @@ def test_parser_all_threads():
     assert options.outdir == "od"
     assert options.clust_mode == 1
     assert not options.spedir
-    assert options.threads == nb
+    assert options.threads == nb_cpu
     assert not options.outfile
     assert options.verbose == 0
     assert not options.quiet
diff --git a/test/test_functional/test_prepare-parser.py b/test/test_functional/test_prepare-parser.py
index 60f4af50..8b92e159 100644
--- a/test/test_functional/test_prepare-parser.py
+++ b/test/test_functional/test_prepare-parser.py
@@ -6,6 +6,8 @@ Functional tests for the parser of 'prepare' subcommand
 """
 import argparse
 import pytest
+import os
+
 
 from PanACoTA.subcommands import prepare
 
@@ -240,7 +242,10 @@ def test_parser_more_threads(capsys):
     error message.
     """
     import multiprocessing
-    nb_cpu = multiprocessing.cpu_count()
+    try:
+        nb_cpu = len(os.sched_getaffinity(0))
+    except AttributeError:
+        nb_cpu = multiprocessing.cpu_count()
     parser = argparse.ArgumentParser(description="Prepare", add_help=False)
     prepare.build_parser(parser)
     with pytest.raises(SystemExit):
@@ -256,7 +261,10 @@ def test_parser_all_threads(capsys):
     error message.
     """
     import multiprocessing
-    nb_cpu = multiprocessing.cpu_count()
+    try:
+        nb_cpu = len(os.sched_getaffinity(0))
+    except AttributeError:
+        nb_cpu = multiprocessing.cpu_count()
     parser = argparse.ArgumentParser(description="Prepare", add_help=False)
     prepare.build_parser(parser)
     options = prepare.parse(parser, "-p 0 --norefseq -o toto".split())
diff --git a/test/test_functional/test_tree-parser.py b/test/test_functional/test_tree-parser.py
index 04ac3a16..74f99d54 100755
--- a/test/test_functional/test_tree-parser.py
+++ b/test/test_functional/test_tree-parser.py
@@ -6,6 +6,7 @@ Functional tests for the parser of tree subcommand
 """
 import argparse
 import pytest
+import os
 
 from PanACoTA.subcommands import tree
 
@@ -50,14 +51,17 @@ def test_parser_thread_toomany(capsys):
     it returns the expected error message
     """
     import multiprocessing
-    nb = multiprocessing.cpu_count()
+    try:
+        nb_cpu = len(os.sched_getaffinity(0))
+    except AttributeError:
+        nb_cpu = multiprocessing.cpu_count()
     parser = argparse.ArgumentParser(description="Tree", add_help=False)
     tree.build_parser(parser)
     with pytest.raises(SystemExit):
-        tree.parse(parser, "-a align --threads {} -o outdir".format(nb + 3).split())
+        tree.parse(parser, "-a align --threads {} -o outdir".format(nb_cpu + 3).split())
     _, err = capsys.readouterr()
     assert ("You have {} threads on your computer, you cannot ask for more: "
-            "invalid value: {}".format(nb, nb+3)) in err
+            "invalid value: {}".format(nb_cpu, nb_cpu+3)) in err
 
 
 def test_parser_thread_neg(capsys):
@@ -309,7 +313,10 @@ def test_parser_all_threads():
     for all arguments
     """
     import multiprocessing
-    nb = multiprocessing.cpu_count()
+    try:
+        nb_cpu = len(os.sched_getaffinity(0))
+    except AttributeError:
+        nb_cpu = multiprocessing.cpu_count()
     parser = argparse.ArgumentParser(description="Tree", add_help=False)
     tree.build_parser(parser)
     args = tree.parse(parser, "-a align -o outdir --threads 0".split())
@@ -319,7 +326,7 @@ def test_parser_all_threads():
     assert args.soft == "iqtree2"
     assert args.model == "GTR"
     assert args.write_boot is False
-    assert args.threads == nb
+    assert args.threads == nb_cpu
     assert args.verbose == 0
     assert args.quiet == False
 
@@ -332,15 +339,18 @@ def test_parser_threads_ok():
     parser = argparse.ArgumentParser(description="Tree", add_help=False)
     tree.build_parser(parser)
     import multiprocessing
-    nb = multiprocessing.cpu_count()
-    args = tree.parse(parser, f"-a align -o outdir --threads {nb}".split())
+    try:
+        nb_cpu = len(os.sched_getaffinity(0))
+    except AttributeError:
+        nb_cpu = multiprocessing.cpu_count()
+    args = tree.parse(parser, f"-a align -o outdir --threads {nb_cpu}".split())
     assert args.alignment == "align"
     assert args.boot is None
     assert args.outdir == "outdir"
     assert args.soft == "iqtree2"
     assert args.model == "GTR"
     assert args.write_boot is False
-    assert args.threads == nb
+    assert args.threads == nb_cpu
     assert args.verbose == 0
     assert args.quiet == False
 
diff --git a/test/test_unit/test_utils-argparse.py b/test/test_unit/test_utils-argparse.py
index d6db74f0..f771552d 100644
--- a/test/test_unit/test_utils-argparse.py
+++ b/test/test_unit/test_utils-argparse.py
@@ -88,7 +88,10 @@ def test_thread_num():
     with pytest.raises(argparse.ArgumentTypeError) as err:
         a = autils.thread_num("a")
     assert ("argument --threads threads: invalid int value: a") in str(err.value)
-    nb_cpu = multiprocessing.cpu_count()
+    try:
+        nb_cpu = len(os.sched_getaffinity(0))
+    except AttributeError:
+        nb_cpu = multiprocessing.cpu_count()
     with pytest.raises(argparse.ArgumentTypeError) as err:
         a = autils.thread_num(str(nb_cpu*2))
     assert (f"You have {nb_cpu} threads on your computer, you cannot ask for more: invalid value: "
-- 
GitLab