From ec09127ca3fbc3e3492e551b4a6e2716c382c1de Mon Sep 17 00:00:00 2001
From: Ian Wienand <iwienand@redhat.com>
Date: Mon, 24 Feb 2020 17:34:31 +1100
Subject: [PATCH] install-javascript-packages: add tox_constraints_file

I can't exactly say why, but the horizon postinstall in package.json
runs "tox -e npm" [1].  This then installs from requirements [2].

The requirements gate run a cross-horizon-npm job that ends up calling
this.  The short story is that if you modify upper-constraints.txt in
a requirements change, it is not picked up by this job.

Add tox_constraints_file to this job, so we can set the path to the
zuul checked-out version for the test.

[1] https://opendev.org/openstack/horizon/src/commit/e515713084f0ce5d7874ffe4ef080fcc2e5760cb/package.json#L22
[2] https://opendev.org/openstack/horizon/src/commit/e515713084f0ce5d7874ffe4ef080fcc2e5760cb/tox.ini#L23

Needed-By: https://review.opendev.org/709389
Change-Id: Ia7dd5af19b42c2ab53dfb70f92e184b0dc215d03
---
 roles/install-javascript-packages/README.rst  |  6 +++++
 .../defaults/main.yaml                        |  2 ++
 .../tasks/main.yaml                           | 25 +++++++++++++++++--
 3 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/roles/install-javascript-packages/README.rst b/roles/install-javascript-packages/README.rst
index 96bb947ac..529c9dfcb 100644
--- a/roles/install-javascript-packages/README.rst
+++ b/roles/install-javascript-packages/README.rst
@@ -6,3 +6,9 @@ Install javascript dependencies needed for a project
    :default: {{ zuul.project.src_dir }}
 
    The directory to work in.
+
+.. zuul:rolevar:: tox_constraints_file
+
+   Path to a pip constraints file. Will set the
+   ``UPPER_CONSTRAINTS_FILE`` environment variable.  Useful if npm
+   ``postinstall`` runs tox.
diff --git a/roles/install-javascript-packages/defaults/main.yaml b/roles/install-javascript-packages/defaults/main.yaml
index 9739eb171..79e5a2e91 100644
--- a/roles/install-javascript-packages/defaults/main.yaml
+++ b/roles/install-javascript-packages/defaults/main.yaml
@@ -1 +1,3 @@
+npm_environment:
+  DISPLAY: ':99'
 zuul_work_dir: "{{ zuul.project.src_dir }}"
diff --git a/roles/install-javascript-packages/tasks/main.yaml b/roles/install-javascript-packages/tasks/main.yaml
index 3c56b89f9..70f3443f8 100644
--- a/roles/install-javascript-packages/tasks/main.yaml
+++ b/roles/install-javascript-packages/tasks/main.yaml
@@ -1,3 +1,25 @@
+- name: Check to see if the constraints file exists
+  stat:
+    path: "{{ tox_constraints_file }}"
+    get_checksum: false
+    get_mime: false
+    get_md5: false
+  register: stat_results
+  when: tox_constraints_file is defined
+
+- name: Fail if constraints file is missing
+  when: tox_constraints_file is defined and not stat_results.stat.exists
+  fail:
+    msg: tox_constraints_file is defined but was not found
+
+- name: Record file location
+  set_fact:
+    tox_constraints_env:
+      TOX_CONSTRAINTS_FILE: "{{ tox_constraints_file }}"
+      # Backward compatibility, to be removed
+      UPPER_CONSTRAINTS_FILE: "{{ tox_constraints_file }}"
+  when: tox_constraints_file is defined
+
 - name: Check for yarn.lock file
   stat:
     path: "{{ zuul_work_dir }}/yarn.lock"
@@ -16,8 +38,7 @@
 
 - name: Install npm dependencies
   command: npm install --verbose
-  environment:
-    DISPLAY: ':99'
+  environment: "{{ npm_environment|combine(tox_constraints_env|default({})) }}"
   args:
     chdir: "{{ zuul_work_dir }}"
   when: not yarn_lock.stat.exists