Set default location for .netrc file for Windows

Fixes https://github.com/bazelbuild/bazel/issues/10867
Also enabled skylark_repository_test on Windows

Closes #10915.

RELNOTES: Set default .netrc file on Windows to %USERPROFILE%
PiperOrigin-RevId: 300073933
diff --git a/src/test/shell/bazel/BUILD b/src/test/shell/bazel/BUILD
index d47e4d4..12448cd1 100644
--- a/src/test/shell/bazel/BUILD
+++ b/src/test/shell/bazel/BUILD
@@ -729,7 +729,6 @@
     ],
     shard_count = 6,
     tags = [
-        "no_windows",
         "requires-network",
     ],
 )
diff --git a/src/test/shell/bazel/skylark_repository_test.sh b/src/test/shell/bazel/skylark_repository_test.sh
index 3bf128d..aeea7d6 100755
--- a/src/test/shell/bazel/skylark_repository_test.sh
+++ b/src/test/shell/bazel/skylark_repository_test.sh
@@ -473,7 +473,8 @@
   result = repository_ctx.execute(
     [str(repository_ctx.which("bash")), "-c", "echo PWD=\$PWD TOTO=\$TOTO"],
     1000000,
-    { "TOTO": "titi" })
+    { "TOTO": "titi" },
+    working_directory = "$repo2")
   if result.return_code != 0:
     fail("Incorrect return code from bash: %s != 0\n%s" % (result.return_code, result.stderr))
   print(result.stdout)
@@ -481,6 +482,9 @@
 EOF
 
   bazel build @foo//:bar >& $TEST_log || fail "Failed to build"
+  if "$is_windows"; then
+    repo2="$(cygpath $repo2)"
+  fi
   expect_log "PWD=$repo2 TOTO=titi"
 }
 
@@ -974,6 +978,11 @@
 }
 
 function test_skylark_repository_executable_flag() {
+  if "$is_windows"; then
+    # There is no executable flag on Windows.
+    echo "Skipping test_skylark_repository_executable_flag on Windows"
+    return
+  fi
   setup_skylark_repository
 
   # Our custom repository rule
@@ -1039,6 +1048,12 @@
   diff "${output_base}/external/foo/download_executable_file.sh" \
     "${download_executable_file}" >/dev/null \
     || fail "download_executable_file.sh is not downloaded successfully"
+
+  # No executable flag for file on Windows
+  if "$is_windows"; then
+    return
+  fi
+
   # Test executable
   test ! -x "${output_base}/external/foo/download_with_sha256.txt" \
     || fail "download_with_sha256.txt is executable"
@@ -1069,6 +1084,12 @@
   # Start HTTP server with Python
   startup_server "${server_dir}"
 
+  # On Windows, a file url should be file:///C:/foo/bar,
+  # we need to add one more slash at the beginning.
+  if "$is_windows"; then
+    server_dir="/${server_dir}"
+  fi
+
   setup_skylark_repository
   # Our custom repository rule
   cat >test.bzl <<EOF
@@ -1711,10 +1732,16 @@
   attrs = {"path": attr.string()},
 )
 EOF
+
+  netrc_dir="$(pwd)"
+  if "$is_windows"; then
+    netrc_dir="$(cygpath -m ${netrc_dir})"
+  fi
+
   cat >> $(create_workspace_with_default_repos WORKSPACE) <<EOF
 load("//:def.bzl", "netrcrepo")
 
-netrcrepo(name = "netrc", path="$(pwd)/.netrc")
+netrcrepo(name = "netrc", path="${netrc_dir}/.netrc")
 EOF
   # ...and that from the parse result, we can read off the
   # credentials for example.com.
@@ -1802,12 +1829,18 @@
   },
 )
 EOF
+
+  netrc_dir="$(pwd)"
+  if "$is_windows"; then
+    netrc_dir="$(cygpath -m ${netrc_dir})"
+  fi
+
   cat >> $(create_workspace_with_default_repos WORKSPACE) <<EOF
 load("//:def.bzl", "authrepo")
 
 authrepo(
   name = "auth",
-  path="$(pwd)/.netrc",
+  path="${netrc_dir}/.netrc",
   urls = [
     "http://example.org/public/null.tar",
     "https://foo.example.org/file1.tar",
@@ -1922,12 +1955,16 @@
   tar cvf x.tar x
   sha256=$(sha256sum x.tar | head -c 64)
   serve_file_auth x.tar
+  netrc_dir="$(pwd)"
+  if "$is_windows"; then
+    netrc_dir="$(cygpath -m ${netrc_dir})"
+  fi
   cat > WORKSPACE <<EOF
 load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
 http_archive(
   name="ext",
   url = "http://127.0.0.1:$nc_port/x.tar",
-  netrc = "$(pwd)/.netrc",
+  netrc = "${netrc_dir}/.netrc",
   sha256="$sha256",
 )
 EOF
@@ -1955,12 +1992,16 @@
   tar cvf x.tar x
   sha256=$(sha256sum x.tar | head -c 64)
   serve_file_auth x.tar
+  netrc_dir="$(pwd)"
+  if "$is_windows"; then
+    netrc_dir="$(cygpath -m ${netrc_dir})"
+  fi
   cat > WORKSPACE <<EOF
 load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
 http_archive(
   name="ext",
   url = "http://127.0.0.1:$nc_port/x.tar",
-  netrc = "$(pwd)/.netrc",
+  netrc = "${netrc_dir}/.netrc",
   sha256="$sha256",
   auth_patterns = {
     "127.0.0.1": "Bearer <password>"
@@ -1992,6 +2033,9 @@
   serve_file_auth x.tar
 
   export HOME=`pwd`
+  if "$is_windows"; then
+    export USERPROFILE="$(cygpath -m ${HOME})"
+  fi
   cat > .netrc <<'EOF'
 machine 127.0.0.1
 login foo
diff --git a/src/test/shell/bazel/testing_server.py b/src/test/shell/bazel/testing_server.py
index b84f100..9d6cd23 100644
--- a/src/test/shell/bazel/testing_server.py
+++ b/src/test/shell/bazel/testing_server.py
@@ -26,11 +26,13 @@
 import os.path
 try:
   from socketserver import TCPServer
-  from socketserver import UnixStreamServer
+  if os.name != 'nt':
+    from socketserver import UnixStreamServer
 except ImportError:
   # Python 2.x compatibility hack.
   from SocketServer import TCPServer
-  from SocketServer import UnixStreamServer
+  if os.name != 'nt':
+    from SocketServer import UnixStreamServer
 import random
 import socket
 import sys
diff --git a/src/test/shell/testenv.sh b/src/test/shell/testenv.sh
index d297c86..1a92fd7 100755
--- a/src/test/shell/testenv.sh
+++ b/src/test/shell/testenv.sh
@@ -106,7 +106,7 @@
 
 # Test data
 testdata_path=${BAZEL_RUNFILES}/src/test/shell/bazel/testdata
-python_server="${BAZEL_RUNFILES}/src/test/shell/bazel/testing_server.py"
+python_server="$(rlocation io_bazel/src/test/shell/bazel/testing_server.py)"
 
 # Third-party
 protoc_compiler="${BAZEL_RUNFILES}/src/test/shell/integration/protoc"
diff --git a/tools/build_defs/repo/http.bzl b/tools/build_defs/repo/http.bzl
index 587dff0..04efba8 100644
--- a/tools/build_defs/repo/http.bzl
+++ b/tools/build_defs/repo/http.bzl
@@ -45,14 +45,17 @@
         netrc = read_netrc(ctx, ctx.attr.netrc)
         return use_netrc(netrc, urls, ctx.attr.auth_patterns)
 
-    if "HOME" in ctx.os.environ:
-        if not ctx.os.name.startswith("windows"):
-            netrcfile = "%s/.netrc" % (ctx.os.environ["HOME"],)
-            if ctx.execute(["test", "-f", netrcfile]).return_code == 0:
-                netrc = read_netrc(ctx, netrcfile)
-                return use_netrc(netrc, urls, ctx.attr.auth_patterns)
+    if "HOME" in ctx.os.environ and not ctx.os.name.startswith("windows"):
+        netrcfile = "%s/.netrc" % (ctx.os.environ["HOME"])
+        if ctx.execute(["test", "-f", netrcfile]).return_code == 0:
+            netrc = read_netrc(ctx, netrcfile)
+            return use_netrc(netrc, urls, ctx.attr.auth_patterns)
 
-    # TODO: Search at a similarly canonical place for Windows as well
+    if "USERPROFILE" in ctx.os.environ and ctx.os.name.startswith("windows"):
+        netrcfile = "%s/.netrc" % (ctx.os.environ["USERPROFILE"])
+        if ctx.path(netrcfile).exists:
+            netrc = read_netrc(ctx, netrcfile)
+            return use_netrc(netrc, urls, ctx.attr.auth_patterns)
 
     return {}