测试完成后如何返回已更改的时区? [英] How is it possible to return back the changed timezone after the completion of tests?

查看:44
本文介绍了测试完成后如何返回已更改的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个项目,在该项目中,我需要将时区更改为UTC,以便仅在考虑时区UTC的情况下运行测试.所以我创建了这个批处理代码:

I was creating a project in which I needed to change the timezone to UTC so that tests run considering the timezone UTC only. So I have created this batch code:

FOR /F "tokens=* USEBACKQ" %%F IN (`tzutil /g`) DO SET PREVIOUS_TZ=%%F
tzutil /s "UTC"
cmd.exe /c npm run test:unit:base
tzutil /s "%PREVIOUS_TZ%"

我想知道完成测试后如何从UTC返回上一个时区.我发现我们可以使用"posttest"来挂钩测试的完成.因此,再次如何使用批处理脚本返回上一个时区?

I wonder how is it possible to return the previous timezone from UTC after the completion of tests. I found out that we can use "posttest" to hook the completion of tests. Thus, again how can we return the previous timezone with batch script?

推荐答案

似乎您已经有 UTC ,该代码是在初始测试中设置的,并且已继承.像这样的东西.

It seems that you already had UTC which was set with your initial test and it was inherited.. anyway, I would rather do something like this.

我会使用 delims = 而不是 tokens = * 设置变量,最好通过在属性的开头到结尾用双引号将它们括起来来完成价值.即设置"PREVIOUS_TZ = %% f" 我修改了变量名以更适合我的感觉.

I would use delims= instead of tokens=* Setting of variables is best done by closing them in double quotes starting at the beginning of the property, to the end of its value. i.e set "PREVIOUS_TZ=%%f" I amended the variable names to better suit my feel.

@echo off
for /F "delims=" %%f in ('tzutil /g') do set "oldtz=%%f"
tzutil /s "UTC"
cmd.exe /c npm run test:unit:base
tzutil /s "%oldtz%"
for /F "delims=" %%a in ('tzutil /g') do if not "%oldtz%" == "%%a" (
    echo Old timezone of "%oldtz%" was not set successfully. It is still %%a
)
pause

这应该每次设置都很好,但是如果没有,它将告诉您.

This should set it perfectly fine each time, but in the event it does not, it will tell you about it.

这篇关于测试完成后如何返回已更改的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆