Nest steps in CIPD package installation

Change-Id: I0478e9ed627eee63d91e03dae67d3f10bf322b9b
Reviewed-on: https://pigweed-review.googlesource.com/c/infra/recipes/+/14560
Reviewed-by: Marc-Antoine Ruel <maruel@google.com>
Commit-Queue: Rob Mohr <mohrr@google.com>
diff --git a/recipe_modules/environment/api.py b/recipe_modules/environment/api.py
index 7ec0ace..76a2d10 100644
--- a/recipe_modules/environment/api.py
+++ b/recipe_modules/environment/api.py
@@ -63,29 +63,31 @@
       for json_path in self._convert_paths(checkout_root,
                                            self._cipd_package_files):
         name = self.m.path.splitext(self.m.path.basename(json_path))[0]
-        install_dir = self._cipd_dir.join(name)
-        self._env['PW_{}_CIPD_INSTALL_DIR'.format(name.upper())] = install_dir
+        with self.m.step.nest(name):
+          install_dir = self._cipd_dir.join(name)
+          self._env['PW_{}_CIPD_INSTALL_DIR'.format(name.upper())] = install_dir
 
-        packages = self.m.file.read_json('read {}'.format(json_path), json_path)
-        if not packages:
-          continue
+          packages = self.m.file.read_json('read {}'.format(json_path),
+                                           json_path)
+          if not packages:
+            continue
 
-        ensure_file = self.m.cipd.EnsureFile()
-        for pkg in packages:
-          # JSON files are read as unicode, need to encode to get str.
-          ensure_file.add_package(pkg['path'].encode(),
-                                  ' '.join(x.encode() for x in pkg['tags']))
-        self.m.cipd.ensure(install_dir, ensure_file)
+          ensure_file = self.m.cipd.EnsureFile()
+          for pkg in packages:
+            # JSON files are read as unicode, need to encode to get str.
+            ensure_file.add_package(pkg['path'].encode(),
+                                    ' '.join(x.encode() for x in pkg['tags']))
+          self.m.cipd.ensure(install_dir, ensure_file)
 
-        for path in ('', 'bin', 'mingw64/bin'):
+          for path in ('', 'bin', 'mingw64/bin'):
             path = install_dir.join(*path.split('/'))
             if self.m.path.exists(path):
-                self._path_prefixes.append(path)
+              self._path_prefixes.append(path)
 
-        for path in ('', 'lib'):
+          for path in ('', 'lib'):
             path = install_dir.join(*path.split('/'))
             if self.m.path.exists(path):
-                self._ldpath_prefixes.append(path)
+              self._ldpath_prefixes.append(path)
 
         self._cipd_installation_dirs.append(install_dir)
 
diff --git a/recipe_modules/environment/test_api.py b/recipe_modules/environment/test_api.py
index b8a2438..7bbda87 100644
--- a/recipe_modules/environment/test_api.py
+++ b/recipe_modules/environment/test_api.py
@@ -13,6 +13,8 @@
 # the License.
 """Test API for enviroment."""
 
+import re
+
 from recipe_engine import recipe_test_api
 
 
@@ -33,9 +35,12 @@
 
   def cipd_test_data(self, sep):
     path = self._convert_path(self._cipd_package_file, sep)
+    # Using re instead of os.path because this needs to handle both Windows and
+    # Linux paths.
+    name = re.search(r'^.*[/\\](.*?)(?:\.json)', path).group(1)
 
     return self.step_data(
-        'environment.setup cipd.read {path}'.format(path=path),
+        'environment.setup cipd.{}.read {}'.format(name, path),
         self.m.file.read_json([
             {
                 'path': 'cipd{sep}path{sep}${{platform}}'.format(sep=sep),
diff --git a/recipe_modules/environment/tests/full.expected/windows.json b/recipe_modules/environment/tests/full.expected/windows.json
index 3281df6..c02ed6a 100644
--- a/recipe_modules/environment/tests/full.expected/windows.json
+++ b/recipe_modules/environment/tests/full.expected/windows.json
@@ -11,6 +11,13 @@
     ]
   },
   {
+    "cmd": [],
+    "name": "environment.setup cipd.cipd_packages",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@"
+    ]
+  },
+  {
     "cmd": [
       "vpython",
       "-u",
@@ -22,9 +29,9 @@
       "/path/to/tmp/"
     ],
     "infra_step": true,
-    "name": "environment.setup cipd.read [START_DIR]\\config\\cipd_packages.json",
+    "name": "environment.setup cipd.cipd_packages.read [START_DIR]\\config\\cipd_packages.json",
     "~followup_annotations": [
-      "@@@STEP_NEST_LEVEL@2@@@",
+      "@@@STEP_NEST_LEVEL@3@@@",
       "@@@STEP_LOG_LINE@cipd_packages.json@[{\"path\": \"cipd\\\\path\\\\${platform}\", \"tags\": [\"version:42\"]}]@@@",
       "@@@STEP_LOG_END@cipd_packages.json@@@"
     ]
@@ -42,9 +49,9 @@
       "-json-output",
       "/path/to/tmp/json"
     ],
-    "name": "environment.setup cipd.ensure_installed",
+    "name": "environment.setup cipd.cipd_packages.ensure_installed",
     "~followup_annotations": [
-      "@@@STEP_NEST_LEVEL@2@@@",
+      "@@@STEP_NEST_LEVEL@3@@@",
       "@@@STEP_LOG_LINE@json.output@{@@@",
       "@@@STEP_LOG_LINE@json.output@  \"result\": {@@@",
       "@@@STEP_LOG_LINE@json.output@    \"\": [@@@",
diff --git a/recipe_modules/environment/tests/full.expected/with_cargo.json b/recipe_modules/environment/tests/full.expected/with_cargo.json
index c227dca..556e494 100644
--- a/recipe_modules/environment/tests/full.expected/with_cargo.json
+++ b/recipe_modules/environment/tests/full.expected/with_cargo.json
@@ -11,6 +11,13 @@
     ]
   },
   {
+    "cmd": [],
+    "name": "environment.setup cipd.cipd_packages",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@"
+    ]
+  },
+  {
     "cmd": [
       "vpython",
       "-u",
@@ -22,9 +29,9 @@
       "/path/to/tmp/"
     ],
     "infra_step": true,
-    "name": "environment.setup cipd.read [START_DIR]/config/cipd_packages.json",
+    "name": "environment.setup cipd.cipd_packages.read [START_DIR]/config/cipd_packages.json",
     "~followup_annotations": [
-      "@@@STEP_NEST_LEVEL@2@@@",
+      "@@@STEP_NEST_LEVEL@3@@@",
       "@@@STEP_LOG_LINE@cipd_packages.json@[{\"path\": \"cipd/path/${platform}\", \"tags\": [\"version:42\"]}]@@@",
       "@@@STEP_LOG_END@cipd_packages.json@@@"
     ]
@@ -42,9 +49,9 @@
       "-json-output",
       "/path/to/tmp/json"
     ],
-    "name": "environment.setup cipd.ensure_installed",
+    "name": "environment.setup cipd.cipd_packages.ensure_installed",
     "~followup_annotations": [
-      "@@@STEP_NEST_LEVEL@2@@@",
+      "@@@STEP_NEST_LEVEL@3@@@",
       "@@@STEP_LOG_LINE@json.output@{@@@",
       "@@@STEP_LOG_LINE@json.output@  \"result\": {@@@",
       "@@@STEP_LOG_LINE@json.output@    \"\": [@@@",
@@ -59,6 +66,13 @@
     ]
   },
   {
+    "cmd": [],
+    "name": "environment.setup cipd.empty",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@"
+    ]
+  },
+  {
     "cmd": [
       "vpython",
       "-u",
@@ -70,9 +84,9 @@
       "/path/to/tmp/"
     ],
     "infra_step": true,
-    "name": "environment.setup cipd.read [START_DIR]/empty.json",
+    "name": "environment.setup cipd.empty.read [START_DIR]/empty.json",
     "~followup_annotations": [
-      "@@@STEP_NEST_LEVEL@2@@@",
+      "@@@STEP_NEST_LEVEL@3@@@",
       "@@@STEP_LOG_LINE@empty.json@\"\"@@@",
       "@@@STEP_LOG_END@empty.json@@@"
     ]